package cn.com.nes.site.module.dao;

import cn.com.nes.common.util.DBUtils;
import cn.com.nes.site.entity.DataReq;
import cn.com.nes.site.entity.PageData;
import cn.com.nes.site.entity.ShowData;
import cn.com.nes.site.entity.UserInfo;
import cn.com.nes.site.module.dao.inventory.GoodsDaoImpl;
import cn.com.nes.site.module.service.contract.ContractService;
import cn.hutool.core.lang.UUID;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.TransactionStatus;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Repository
public class PointsDaoImpl extends BaseDaoImpl {
	@Resource
    private ContractService contractService;
	@Resource
	private GoodsDaoImpl goodsDaoImpl;
	@Resource
	private CompanyDaoImpl companyDaoImpl;
	
	private TransactionStatus newg_status = null;
//插入记录表
    public void insertDemodata(ShowData data) {
    	StringBuffer sb = new StringBuffer();
    	sb.delete(0, sb.length());
    	sb.append(" INSERT INTO d_demodata ( tablename, `key`, keyvalue, demoKeyvalue, companyid, userid, democompanyid, demouserid, keytype)"
    			+ " VALUES ( '"+data.getTablename()+"', '"+data.getKey()+"', '"+data.getKeyvalue()+"', '"+data.getDemoKeyvalue()
    			+"', '"+data.getCompanyid()+"', '"+data.getUserid()+"', '"+data.getDemocompanyid()+"', '"+data.getDemouserid()
    			+"', '"+data.getKeytype()+"'); ");
    	this.getJdbcTemplate_newg().update(sb.toString());

    }
  //查询键值对应的数值
    public String  queryKeyValue(ShowData data) {
    	String keyvalue="";
    	StringBuffer sb = new StringBuffer();
    	sb.delete(0, sb.length());
    	sb.append(" select  keyvalue from d_demodata"
    			+ " where  tablename ='"+data.getTablename()+"' and `key`= '"+data.getKey()+"' and  demoKeyvalue='"+data.getDemoKeyvalue()
    			+"' and  democompanyid = '"+data.getDemocompanyid()+"' and demouserid ='"+data.getDemouserid()
    			+"'; ");
    	List<Map<String, Object>> List = this.getJdbcTemplate_newg().queryForList(sb.toString());
    	if(List.size()>0)
    	{
    		keyvalue = (String)List.get(0).get("keyvalue");
    	}
    	return keyvalue;

    }
  //用户注册时候，创建默认数据
  	public PageData createDemoData(String companyIdDemo,String useridDemo,String companyType,String companyIdnew,String companyNamenew,String useridnew,String usernamenew) {
  		PageData res = new PageData();
  		StringBuffer sb = new StringBuffer();
  		StringBuffer sb1 = new StringBuffer();
  		StringBuffer sb2 = new StringBuffer();
//  		String companyIdDemo="00000016";
//  		String companyIdnew="00000041";
//  		String companyNamenew="济南新能源建设有新公司";
//  	    String useridDemo="615";//模板管理用户id
//  		String useridnew="878";//新建演示公司管理用户id
//  		String usernamenew="演示用户";//新建演示公司管理用户id
//  		String companyType="02";
  		
  		
  		
  		//删除demo数据
 				
  		companyDaoImpl.deleteAllCompany(companyIdnew);
  		
  		//插入默认的公司和用户
  		ShowData data_user=new ShowData();
		data_user.setTablename("t_user");
		data_user.setKey("id");
		data_user.setKeyvalue(useridnew);
		data_user.setDemoKeyvalue(useridDemo);
		data_user.setCompanyid(companyIdnew);
		data_user.setUserid(useridnew);
		data_user.setDemocompanyid(companyIdDemo);
		data_user.setDemouserid(useridDemo);
		data_user.setKeytype("mainkey");
		insertDemodata(data_user);
		
		ShowData data_com=new ShowData();
		data_com.setTablename("t_company");
		data_com.setKey("companyId");
		data_com.setKeyvalue(companyIdnew);
		data_com.setDemoKeyvalue(companyIdDemo);
		data_com.setCompanyid(companyIdnew);
		data_com.setUserid(useridnew);
		data_com.setDemocompanyid(companyIdDemo);
		data_com.setDemouserid(useridDemo);
		data_com.setKeytype("mainkey");
		insertDemodata(data_com);
  		
  		//创建角色
  		sb.delete(0, sb.length());
		sb.append("select role_id from t_role where companyid='"+companyIdDemo+"'  and sfsc='0' ");
		List<Map<String, Object>> roleIdList = this.getJdbcTemplate_newg().queryForList(sb.toString(), new Object[]{});
		//插入角色
		 for (int i=0;i<roleIdList.size();i++) {
			 String roleIdDemo=(String)roleIdList.get(i).get("role_id");
			 sb1.delete(0, sb1.length());
			 UUID uuID = UUID.randomUUID();
			 String roleIdnew = uuID.toString();
		   	 sb1.append("insert into t_role(role_id, `name`, `desc`, enabled, isAuthorRole, companyId, sfsc, createUid, createTime, modifyUid, modifyTime)  "
		  				+ "select '"+roleIdnew+"', `name`, `desc`, enabled, isAuthorRole, '"+companyIdnew+"', sfsc, '"+useridnew+"', now(), '"+useridnew+"', now()  from t_role where companyid='"+companyIdDemo+"' and role_id='"+roleIdDemo+"'");
			this.getJdbcTemplate_newg().update(sb1.toString());
			ShowData data=new ShowData();
			data.setTablename("t_role");
			data.setKey("role_id");
			data.setKeyvalue(roleIdnew);
			data.setDemoKeyvalue(roleIdDemo);
			data.setCompanyid(companyIdnew);
			data.setUserid(useridnew);
			data.setDemocompanyid(companyIdDemo);
			data.setDemouserid(useridDemo);
			data.setKeytype("mainkey");
			insertDemodata(data);
			 //插入角色相关模块
			sb1.delete(0, sb1.length());
			 sb1.append("INSERT INTO t_role_model (role_id, model_id, sfsc, createUid, createTime, modifyUid, modifyTime)" + 
			 		"select '"+roleIdnew+"', model_id, sfsc, '"+useridnew+"', now(), '"+useridnew+"', now() from t_role_model where role_id='"+roleIdDemo+"'");
			this.getJdbcTemplate_newg().update(sb1.toString());
		 }
		 
		 
		//插入公司下属的子公司（可以不建立）
		 sb.delete(0, sb.length());
		 sb.append("SELECT companyId, companyName, p_companyId, p_companyName, companyType, nes_companyId, sfsc, address, legalPerson, contactNumber, areacode, longitude, latitude, industry, businessType, machineCount, industryMemo, brand, cretUserId, isCert, uniformSocialCreditCode, businessLicense, pbxURL, isShow\n" + 
					" from t_company where p_companyId='"+companyIdDemo+"' and sfsc='0'");
			List<Map<String, Object>> underCompanyIdList = this.getJdbcTemplate_newg().queryForList(sb.toString(), new Object[]{});
			//插入角色
			 for (int i=0;i<underCompanyIdList.size();i++) {
				 String underCompanyIdDemo=(String)underCompanyIdList.get(i).get("companyId");
				 sb1.delete(0, sb1.length());
				 String underCompanyIdnew =  getNextId(companyIdnew);
			   	 sb1.append(" INSERT INTO t_company (companyId, companyName, p_companyId, p_companyName, companyType, nes_companyId, sfsc, address, legalPerson, contactNumber, areacode, longitude, latitude, industry, businessType, machineCount, industryMemo, brand, cretUserId, isCert, uniformSocialCreditCode, businessLicense, pbxURL, isShow) \n" + 
			   	 		"		 SELECT '"+underCompanyIdnew+"', companyName, '"+companyIdnew+"', '"+companyNamenew+"', companyType, nes_companyId, sfsc, address, legalPerson, contactNumber, areacode, longitude, latitude, industry, businessType, machineCount, industryMemo, brand, cretUserId, isCert, uniformSocialCreditCode, businessLicense, pbxURL, isShow\n" + 
			   	 		"		  from t_company where companyId ='"+underCompanyIdDemo+"' ");
				this.getJdbcTemplate_newg().update(sb1.toString());
				ShowData data=new ShowData();
				data.setTablename("t_company");
				data.setKey("companyId");
				data.setKeyvalue(underCompanyIdnew);
				data.setDemoKeyvalue(underCompanyIdDemo);
				data.setCompanyid(companyIdnew);
				data.setUserid(useridnew);
				data.setDemocompanyid(companyIdDemo);
				data.setDemouserid(useridDemo);
				data.setKeytype("mainkey");
				insertDemodata(data);
				
			 }
		 //插入公司下属的代理商或者厂家
		 sb.delete(0, sb.length());
			
			if("01".equals(companyType)){//厂商
				sb.append(" INSERT INTO t_agent_facturer (agent_id, facturer_d, deleted, gmt_create) " + "select agent_id, '" + companyIdnew + "', '0', now() from t_agent_facturer WHERE facturer_id = ? and deleted ='0';");
				this.getJdbcTemplate_newg().update(sb.toString(), new Object[]{companyIdDemo});
			}else if("02".equals(companyType)){//代理商
				sb.append(" INSERT INTO t_agent_facturer (agent_id, facturer_id, deleted, gmt_create) " + "select  '" + companyIdnew + "',facturer_id, '0', now() from t_agent_facturer WHERE agent_id = ? and deleted = '0';");
				this.getJdbcTemplate_newg().update(sb.toString(), new Object[]{companyIdDemo});
			}
		 
		 //部门
		 sb.delete(0, sb.length());
			sb.append("select departmentId, departmentName, p_companyId, p_companyName, sfsc " + 
					" from t_department where p_companyId='"+companyIdDemo+"'  and sfsc='0' ");
			List<Map<String, Object>> DepartmentList = this.getJdbcTemplate_newg().queryForList(sb.toString(), new Object[]{});
		 for (int i=0;i<DepartmentList.size();i++) {
				 String departmentIdDemo=(String)DepartmentList.get(i).get("departmentId");
				 sb1.delete(0, sb1.length());
				 String departmentIdnew = companyIdnew+StringUtils.right(departmentIdDemo,4);
				 sb1.append("INSERT INTO t_department (departmentId, departmentName, p_companyId, p_companyName, sfsc) " + 
				 		"		 select '"+departmentIdnew+"', departmentName, '"+companyIdnew+"', '"+companyNamenew+"', sfsc " + 
				 		"		 from t_department where p_companyId='"+companyIdDemo+"' and departmentId='"+departmentIdDemo+"'   and sfsc='0' ");
				 
				this.getJdbcTemplate_newg().update(sb1.toString());
				ShowData data=new ShowData();
				data.setTablename("t_department");
				data.setKey("departmentId");
				data.setKeyvalue(departmentIdnew);
				data.setDemoKeyvalue(departmentIdDemo);
				data.setCompanyid(companyIdnew);
				data.setUserid(useridnew);
				data.setDemocompanyid(companyIdDemo);
				data.setDemouserid(useridDemo);
				data.setKeytype("mainkey");
				insertDemodata(data);
		}
		 //用户
		 sb.delete(0, sb.length());
			sb.append("select id, uuid, username, password, sex, nickName, role_id, areacode, areaname, companyId, departmentId, identity, userType, openId, phone, email, wechat, longitude, latitude, createUid, createTime, modifyUid, modifyTime, isassociated, relevanceid, sfsc, isOnlineReg, pic, isCert, mfepuser, open_id_dy"
					+ " from t_user where companyId='"+companyIdDemo+"' and id<>'"+useridDemo+"' and sfsc='0' ");
			List<Map<String, Object>> UserList = this.getJdbcTemplate_newg().queryForList(sb.toString(), new Object[]{});
		 for (int i=0;i<UserList.size();i++) {
				 String userid=(String)UserList.get(i).get("uuid");
				 String usernameDemo=(String)UserList.get(i).get("username");
				 sb1.delete(0, sb1.length());
				 UUID uuID = UUID.randomUUID();
				 String userUUIDnew = uuID.toString();
				 //获取对应的role_id
				 ShowData data1=new ShowData();
				data1.setTablename("t_role");
				data1.setKey("role_id");
				data1.setDemoKeyvalue((String)UserList.get(i).get("role_id"));
				data1.setDemocompanyid(companyIdDemo);
				data1.setDemouserid(useridDemo);
				String role_idnew = queryKeyValue(data1);
				
				//获取对应的departmentId
				 ShowData data2=new ShowData();
				data2.setTablename("t_department");
				data2.setKey("departmentId");
				data2.setDemoKeyvalue((String)UserList.get(i).get("departmentId"));
				data2.setDemocompanyid(companyIdDemo);
				data2.setDemouserid(useridDemo);
				String departmentIdnew = queryKeyValue(data2);
				
				sb1.append("INSERT INTO t_user (uuid, username, password, sex, nickName, role_id, areacode, areaname, companyId, departmentId, identity, userType, openId, phone, email, wechat, longitude, latitude, createUid, createTime, modifyUid, modifyTime, isassociated, relevanceid, sfsc, isOnlineReg, pic, isCert, mfepuser, open_id_dy) \n" + 
				 		"select '"+userUUIDnew+"', CONCAT(username,max(id)+1), password, sex, nickName, '"+role_idnew+"', areacode, areaname, '"+companyIdnew+"', '"+departmentIdnew+"', identity, userType, '', phone, email, '', longitude, latitude, '"+useridnew+"', now(), '"+useridnew+"', now(), isassociated, relevanceid, sfsc, isOnlineReg, pic, isCert, mfepuser, open_id_dy\n" + 
				 		" from t_user where companyId='"+companyIdDemo+"' and uuid='"+userid+"' ");
				this.getJdbcTemplate_newg().update(sb1.toString());
				//插入后取回userid
				sb1.delete(0, sb1.length());
				sb1.append("select id,username from  t_user  where companyId='"+companyIdnew+"' and uuid='"+userUUIDnew+"' ");
				List<Map<String, Object>> uidList = this.getJdbcTemplate_newg().queryForList(sb1.toString());
				String uid = "";
				String usernamenew1 = "";
				if(uidList.size()>0) {
					uid = ((Long)uidList.get(0).get("id")).toString();
					usernamenew1 = (String)uidList.get(0).get("username");
					}
				
				//插入到记录表
				ShowData data=new ShowData();
				data.setTablename("t_user");
				data.setKey("id");
				data.setKeyvalue(uid);
				data.setDemoKeyvalue(userid);
				data.setCompanyid(companyIdnew);
				data.setUserid(useridnew);
				data.setDemocompanyid(companyIdDemo);
				data.setDemouserid(useridDemo);
				data.setKeytype("mainkey");
				insertDemodata(data);
				//插入到记录表
				ShowData data3=new ShowData();
				data3.setTablename("t_user");
				data3.setKey("username");
				data3.setKeyvalue(usernamenew1);
				data3.setDemoKeyvalue(usernameDemo);
				data3.setCompanyid(companyIdnew);
				data3.setUserid(useridnew);
				data3.setDemocompanyid(companyIdDemo);
				data3.setDemouserid(useridDemo);
				data3.setKeytype("col");
				insertDemodata(data3);
		}
		 
		 // 插入线索
		 sb.delete(0, sb.length());
	   	 sb.append("select uuid, cluestage, followupstatus, poolentrymode, cluesource, advertisementId, culestate, clueowner, cluename, cluecompany, companycontact, cluephone, numberhome, companyMemo, pooltype, personpost, businessdirection, companyfocus, projectname, personincharge, projectaddress, personphone, xzqhdm, longitude, latitude, sex, wechat, projectarea, picture1, picture2, picture3, memo, serial, openid, cluebelongtocompany, otherdata, createUid, createTime, modifyUid, modifyTime, isdelete, jsondata, templateId, isNew, sfzx, customeraddress, customerjd, customerwd from clue " + 
					"where cluebelongtocompany ='"+companyIdDemo+"' and isdelete='0' ");
			List<Map<String, Object>> ClueList = this.getJdbcTemplate_newg().queryForList(sb.toString(), new Object[]{});
		 for (int i=0;i<ClueList.size();i++) {
				 String clueuuid=(String)ClueList.get(i).get("uuid");
				 sb1.delete(0, sb1.length());
				 UUID uuID = UUID.randomUUID();
				 String clueUUIDnew = uuID.toString();
				 
				 sb1.append("INSERT INTO clue (uuid, cluestage, followupstatus, poolentrymode, cluesource, advertisementId, culestate, clueowner, cluename, cluecompany, companycontact, cluephone, numberhome, companyMemo, pooltype, personpost, businessdirection, companyfocus, projectname, personincharge, projectaddress, personphone, xzqhdm, longitude, latitude, sex, wechat, projectarea, picture1, picture2, picture3, memo,  openid, cluebelongtocompany, otherdata, createUid, createTime, modifyUid, modifyTime, isdelete, jsondata, templateId, isNew, sfzx, customeraddress, customerjd, customerwd) " + 
				 		"select '"+clueUUIDnew+"', cluestage, followupstatus, poolentrymode, cluesource, advertisementId, culestate, clueowner, cluename, cluecompany, companycontact, cluephone, numberhome, companyMemo, pooltype, personpost, businessdirection, companyfocus, projectname, personincharge, projectaddress, personphone, xzqhdm, longitude, latitude, sex, wechat, projectarea, picture1, picture2, picture3, memo, openid, '"+companyIdnew+"', otherdata, createUid, createTime, modifyUid, modifyTime, isdelete, jsondata, templateId, isNew, sfzx, customeraddress, customerjd, customerwd from clue" + 
				 		"  where cluebelongtocompany = '"+companyIdDemo+"' and uuid='"+clueuuid+"' ");
				this.getJdbcTemplate_newg().update(sb1.toString());
				
				
				//插入到记录表
				ShowData data=new ShowData();
				data.setTablename("clue");
				data.setKey("uuid");
				data.setKeyvalue(clueUUIDnew);
				data.setDemoKeyvalue(clueuuid);
				data.setCompanyid(companyIdnew);
				data.setUserid(useridnew);
				data.setDemocompanyid(companyIdDemo);
				data.setDemouserid(useridDemo);
				data.setKeytype("mainkey");
				insertDemodata(data);
		}
		 
		 // 插入客户
		 sb.delete(0, sb.length());
	   	 sb.append("select uuid, companyId, customernature, businesstype, customerlevel, superiorcompany, customername, xzqhdm, address, personincharge, phone, memo, distributorcode, distributortype, channelperson, natureofcompany, legalperson, establishmenttime, unifiedcreditcode, registeredcapital, scopeofbusiness, longitude, latitude, website, createUid, createTime, modifyUid, modifyTime, isdelete, jsondata, templateId, cluecompany, pooltype, companycontact, personpost, businessdirection, companyMemo, openId, activeUuid, sex, clueId " + 
	   	 		"from customer where companyId='"+companyIdDemo+"'  and isdelete='0'  ");
			List<Map<String, Object>> customerList = this.getJdbcTemplate_newg().queryForList(sb.toString(), new Object[]{});
		 for (int i=0;i<customerList.size();i++) {
				 String customeruuid=(String)customerList.get(i).get("uuid");
				//获取对应的clueId
				 ShowData data1=new ShowData();
				data1.setTablename("clue");
				data1.setKey("uuid");
				data1.setDemoKeyvalue((String)customerList.get(i).get("clueId"));
				data1.setDemocompanyid(companyIdDemo);
				data1.setDemouserid(useridDemo);
				String clueIdnew = queryKeyValue(data1);
				
				 sb1.delete(0, sb1.length());
				 UUID uuID = UUID.randomUUID();
				 String customeruuidnew = uuID.toString();
				 
				 sb1.append("INSERT INTO customer (uuid, companyId, customernature, businesstype, customerlevel, superiorcompany, customername, xzqhdm, address, personincharge, phone, memo, distributorcode, distributortype, channelperson, natureofcompany, legalperson, establishmenttime, unifiedcreditcode, registeredcapital, scopeofbusiness, longitude, latitude, website, createUid, createTime, modifyUid, modifyTime, isdelete, jsondata, templateId, cluecompany, pooltype, companycontact, personpost, businessdirection, companyMemo, openId, activeUuid, sex, clueId) " + 
				 		"select '"+customeruuidnew+"', '"+companyIdnew+"', customernature, businesstype, customerlevel, superiorcompany, customername, xzqhdm, address, personincharge, phone, memo, distributorcode, distributortype, channelperson, natureofcompany, legalperson, establishmenttime, unifiedcreditcode, registeredcapital, scopeofbusiness, longitude, latitude, website, createUid, createTime, modifyUid, modifyTime, isdelete, jsondata, templateId, cluecompany, pooltype, companycontact, personpost, businessdirection, companyMemo, openId, activeUuid, sex, '"+clueIdnew+"' " + 
				 		"from customer where companyId= '"+companyIdDemo+"' and uuid='"+customeruuid+"' ");
				this.getJdbcTemplate_newg().update(sb1.toString());
				
				//插入到记录表
				ShowData data=new ShowData();
				data.setTablename("customer");
				data.setKey("uuid");
				data.setKeyvalue(customeruuidnew);
				data.setDemoKeyvalue(customeruuid);
				data.setCompanyid(companyIdnew);
				data.setUserid(useridnew);
				data.setDemocompanyid(companyIdDemo);
				data.setDemouserid(useridDemo);
				data.setKeytype("mainkey");
				insertDemodata(data);
				
				//插入到客户附加文件
				 sb1.delete(0, sb1.length());
				 sb1.append("INSERT INTO customer_file (uuid, customerId, filetype, filename, filepath, createTime, createUid, memo, isdelete) " + 
				 		"	select uuid(), '"+customeruuidnew+"', filetype, filename, filepath, now(), '"+useridnew+"', memo, isdelete " + 
				 		"	from customer_file where customerId= '"+customeruuid+"'   and isdelete='0'  ");
				this.getJdbcTemplate_newg().update(sb1.toString());
				
				//插入到客户附加员工
				 sb1.delete(0, sb1.length());
				 sb1.append("INSERT INTO customer_staff (uuid, customerId, department, post, name, sex, phone, ismain, wechat, createUid, createTime, modifyUid, modifyTime, isdelete)  " + 
				 		"select uuid(),  '"+customeruuidnew+"', b.keyvalue, a.post, a.name, sex, phone, ismain, wechat, '"+useridnew+"', now(), '"+useridnew+"', now(), isdelete " + 
				 		"from  customer_staff a left join d_demodata b on a.department = b.demoKeyvalue and b.tablename='t_department' and b.democompanyid='"+companyIdDemo+"' where  a.customerId= '"+customeruuid+"' and a.isdelete='0'   ");
				this.getJdbcTemplate_newg().update(sb1.toString());
		}
		//插入商机
		 sb.delete(0, sb.length());
	   	 sb.append("select uuid, companyId, opportunitystage, opportunitytype, opportunityname, customerId, bimId, clueId, opportunityowner, xzqhdm, projectaddress, projectarea, longitude, latitude, personincharge, sex, wechat, memo, picture1, picture2, picture3, opportunitymoney, estimatedclosingdate, createUid, createTime, modifyUid, modifyTime, isdelete, jsondata, templateId, mobilizationtime, completetime, ordergoodstime, equinumber, buildnature, bulidtype, equispec, equispecsize, cgftype, cgfphone, sjfcompanyname, sjfphone, cjfcompanyname, cjfphone, tzfcompanyname, tzfphone, jlfcompanyname, jlfphone\n" + 
	   	 		"from opportunity where companyId='"+companyIdDemo+"'  and isdelete='0' ");
			List<Map<String, Object>> opportunityList = this.getJdbcTemplate_newg().queryForList(sb.toString(), new Object[]{});
		 for (int i=0;i<opportunityList.size();i++) {
				 String opportunityuuid=(String)opportunityList.get(i).get("uuid");
				//获取对应的customerId
				 ShowData data1=new ShowData();
				data1.setTablename("customerId");
				data1.setKey("uuid");
				data1.setDemoKeyvalue((String)opportunityList.get(i).get("customerId"));
				data1.setDemocompanyid(companyIdDemo);
				data1.setDemouserid(useridDemo);
				String customerIdnew = queryKeyValue(data1);
				//获取对应的clueId
				 ShowData data2=new ShowData();
				 data2.setTablename("clue");
				 data2.setKey("uuid");
				 data2.setDemoKeyvalue((String)opportunityList.get(i).get("clueId"));
				 data2.setDemocompanyid(companyIdDemo);
				 data2.setDemouserid(useridDemo);
				String clueIdnew = queryKeyValue(data2);
				
				
				 sb1.delete(0, sb1.length());
				 UUID uuID = UUID.randomUUID();
				 String opportunityuuidnew = uuID.toString();
				 
				 
				 sb1.append("INSERT INTO opportunity (uuid, companyId, opportunitystage, opportunitytype, opportunityname, customerId, bimId,clueId , opportunityowner, xzqhdm, projectaddress, projectarea, longitude, latitude, personincharge, sex, wechat, memo, picture1, picture2, picture3, opportunitymoney, estimatedclosingdate, createUid, createTime, modifyUid, modifyTime, isdelete, jsondata, templateId, mobilizationtime, completetime, ordergoodstime, equinumber, buildnature, bulidtype, equispec, equispecsize, cgftype, cgfphone, sjfcompanyname, sjfphone, cjfcompanyname, cjfphone, tzfcompanyname, tzfphone, jlfcompanyname, jlfphone)\n" + 
				 		"select '"+opportunityuuidnew+"', '"+companyIdnew+"', opportunitystage, opportunitytype, opportunityname, '"+customerIdnew+"', bimId,'"+clueIdnew+"' , opportunityowner, xzqhdm, projectaddress, projectarea, longitude, latitude, personincharge, sex, wechat, memo, picture1, picture2, picture3, opportunitymoney, estimatedclosingdate, '"+useridnew+"', now(), '"+useridnew+"', now(), isdelete, jsondata, templateId, mobilizationtime, completetime, ordergoodstime, equinumber, buildnature, bulidtype, equispec, equispecsize, cgftype, cgfphone, sjfcompanyname, sjfphone, cjfcompanyname, cjfphone, tzfcompanyname, tzfphone, jlfcompanyname, jlfphone \n" + 
				 		"from opportunity where companyId= '"+companyIdDemo+"' and uuid='"+opportunityuuid+"' ");
				this.getJdbcTemplate_newg().update(sb1.toString());
				
				//插入到记录表
				ShowData data=new ShowData();
				data.setTablename("opportunity");
				data.setKey("uuid");
				data.setKeyvalue(opportunityuuidnew);
				data.setDemoKeyvalue(opportunityuuid);
				data.setCompanyid(companyIdnew);
				data.setUserid(useridnew);
				data.setDemocompanyid(companyIdDemo);
				data.setDemouserid(useridDemo);
				data.setKeytype("mainkey");
				insertDemodata(data);
				
				//插入到opportunity_bim
				 sb1.delete(0, sb1.length());
				 sb1.append("INSERT INTO opportunity_bim (uuid, opportunityId, bimId, tid, uid, title, pic, memo, designUrl, shareUrl, sharePermission, createUid, createTime, modifyUid, modifyTime, isdelete) \n" + 
				 		"select uuid(), '"+opportunityuuidnew+"', bimId, tid, uid, title, pic, memo, designUrl, shareUrl, sharePermission, '"+useridnew+"', now(), '"+useridnew+"', now(), isdelete\n" + 
				 		"from opportunity_bim where  opportunityId ='"+opportunityuuid+"'  and isdelete='0' ");
				this.getJdbcTemplate_newg().update(sb1.toString());
				
				//插入到opportunity_file
				 sb1.delete(0, sb1.length());
				 sb1.append("INSERT INTO opportunity_file (uuid, opportunityId, filetype, filename, filepath, createTime, createUid, memo, isdelete) \n" + 
				 		"select uuid(), '"+opportunityuuidnew+"', filetype, filename, filepath, now(),'"+useridnew+"',  memo, isdelete \n" + 
				 		"from opportunity_file where  opportunityId ='"+opportunityuuid+"'  and isdelete='0' ");
				this.getJdbcTemplate_newg().update(sb1.toString());
				
				//插入到opportunity_staff
				
				 sb1.delete(0, sb1.length());
				 sb1.append("INSERT INTO opportunity_staff (uuid, customerId, opportunityId, customerstaffid, createUid, createTime, isdelete, department, post, name, sex, phone, ismain, wechat)\n" + 
				 		"select uuid(), '"+customerIdnew+"', '"+opportunityuuidnew+"', customerstaffid, '"+useridnew+"', now(), isdelete, department, post, name, sex, phone, ismain, wechat \n" + 
				 		"from opportunity_staff where  opportunityId ='"+opportunityuuid+"'  and isdelete='0' ");
				this.getJdbcTemplate_newg().update(sb1.toString());
		} 
		 
		//插入合同
		
		 sb.delete(0, sb.length());
	   	 sb.append("select id, company_id, contract_number, name, type, company_name, contact_person, contract_amount, creator_id, `terminated`, effective_date, termination_date, audit_state, oss_path, gmt_create, gmt_modified, deleted, file_name, opportunity_id" + 
	   	 		" from contract_info where company_id='"+companyIdDemo+"'  and `deleted`='0' ");
			List<Map<String, Object>> contractList = this.getJdbcTemplate_newg().queryForList(sb.toString(), new Object[]{});
		 for (int i=0;i<contractList.size();i++) {
				 String contractid=(String)contractList.get(i).get("id");
				//获取对应的opportunity_id
				 ShowData data1=new ShowData();
				data1.setTablename("opportunity");
				data1.setKey("uuid");
				data1.setDemoKeyvalue((String)contractList.get(i).get("opportunity_id"));
				data1.setDemocompanyid(companyIdDemo);
				data1.setDemouserid(useridDemo);
				String opportunityIdnew = queryKeyValue(data1);
				
				//获取对应的creator_id
				 ShowData data2=new ShowData();
				 data2.setTablename("t_user");
				 data2.setKey("id");
				 data2.setDemoKeyvalue(((Long)contractList.get(i).get("creator_id")).toString());
				 data2.setDemocompanyid(companyIdDemo);
				 data2.setDemouserid(useridDemo);
				String creator_idnew = queryKeyValue(data2);
				if ("".equals(creator_idnew)) {
					creator_idnew = useridnew;
//					String creator_id =(String)contractList.get(i).get("creator_id");
//					if(creator_id.equals(useridDemo)) {
//						creator_idnew = useridnew;
//					}
				}
				
				
				
				
				 sb1.delete(0, sb1.length());
				 UUID uuID = UUID.randomUUID();
				 String contractuuidnew = uuID.toString();
				 //获取合同编号
				 DataReq req1 = new DataReq();
				 UserInfo useinf =new UserInfo();
				 useinf.setCompanyId(companyIdnew);
				 req1.setUinfo(useinf);
				 String contractNumber = contractService.generateContractNumber(req1);
				 sb1.append("INSERT INTO contract_info (id, company_id, contract_number, name, type, company_name, contact_person, contract_amount, creator_id, `terminated`, effective_date, termination_date, audit_state, oss_path, gmt_create, gmt_modified, deleted, file_name, opportunity_id)" + 
				 		" select '"+contractuuidnew+"','"+companyIdnew+"', '"+contractNumber+"', name, type, company_name, contact_person, contract_amount, "+creator_idnew+", `terminated`, effective_date, termination_date, audit_state, oss_path, gmt_create, gmt_modified, deleted, file_name, '"+opportunityIdnew+"'" + 
				 		" from contract_info where company_id= '"+companyIdDemo+"' and id='"+contractid+"' ");
				this.getJdbcTemplate_newg().update(sb1.toString());
				
				//插入到记录表
				ShowData data=new ShowData();
				data.setTablename("contract_info");
				data.setKey("id");
				data.setKeyvalue(contractuuidnew);
				data.setDemoKeyvalue(contractid);
				data.setCompanyid(companyIdnew);
				data.setUserid(useridnew);
				data.setDemocompanyid(companyIdDemo);
				data.setDemouserid(useridDemo);
				data.setKeytype("mainkey");
				insertDemodata(data);
				
				//插入到contract_audit_record
				 sb1.delete(0, sb1.length());
				 sb1.append("INSERT INTO contract_audit_record (company_id, contract_id, passed, audit_comment, user_id, gmt_create, gmt_modified) \n" + 
				 		"SELECT '"+companyIdnew+"', '"+contractuuidnew+"', passed, audit_comment, '"+useridnew+"', now(), now() \n" + 
				 		"from contract_audit_record where  contract_id ='"+contractid+"'  ");
				this.getJdbcTemplate_newg().update(sb1.toString());
		}
		 
		//插入 商品
		 sb.delete(0, sb.length());
	   	 sb.append("select uuid, goodsCode, goodsName, goodsType, goodsModel, customerId, customerName, brand, packingSize, unit, companyId, createUid, createTime, modifyUid, modifyTime, tabPosition, isdelete, numberHight, numberLow, sblx, sbmc, sbxh\n" + 
	   	 		"from g_goods where companyId='"+companyIdDemo+"'  and isdelete='0' ");
			List<Map<String, Object>> goodsList = this.getJdbcTemplate_newg().queryForList(sb.toString(), new Object[]{});
		 for (int i=0;i<goodsList.size();i++) {
				 String goodsid=(String)goodsList.get(i).get("uuid");
				//获取对应的customerId
				 String customerIdnew="";
				 String customerNamenew ="";
				 if (((String)goodsList.get(i).get("customerId")).equals(companyIdDemo)) {
					 customerIdnew = companyIdnew;
					 customerNamenew=companyNamenew;
				 }else {
					 customerIdnew= (String)goodsList.get(i).get("customerId");
					 customerNamenew=(String)goodsList.get(i).get("customerName");
				 }
				
				 sb1.delete(0, sb1.length());
				 UUID uuID = UUID.randomUUID();
				 String goodsuuidnew = uuID.toString();
				 sb1.append("INSERT INTO g_goods (uuid, goodsCode, goodsName, goodsType, goodsModel, customerId, customerName, brand, packingSize, unit, companyId, createUid, createTime, modifyUid, modifyTime, tabPosition, isdelete, numberHight, numberLow, sblx, sbmc, sbxh) \n" + 
				 		"select '"+goodsuuidnew+"', goodsCode, goodsName, goodsType, goodsModel, '"+customerIdnew+"', '"+customerNamenew+"', brand, packingSize, unit, '"+companyIdnew+"', '"+useridnew+"', now(), '"+useridnew+"', now(), tabPosition, isdelete, numberHight, numberLow, sblx, sbmc, sbxh\n" + 
				 		"from g_goods where companyId= '"+companyIdDemo+"' and uuid='"+goodsid+"' ");
				this.getJdbcTemplate_newg().update(sb1.toString());
				
				//插入到记录表
				ShowData data=new ShowData();
				data.setTablename("g_goods");
				data.setKey("uuid");
				data.setKeyvalue(goodsuuidnew);
				data.setDemoKeyvalue(goodsid);
				data.setCompanyid(companyIdnew);
				data.setUserid(useridnew);
				data.setDemocompanyid(companyIdDemo);
				data.setDemouserid(useridDemo);
				data.setKeytype("mainkey");
				insertDemodata(data);
				
				//插入到g_goodstype
				 sb1.delete(0, sb1.length());
				 sb1.append("INSERT INTO g_goodstype (companyId, goodsTypeId, goodsTypeName, P_goodsTypeId, P_goodsTypeName, memo, createUid, createTime, modifyUid, modifyTime, isdelete, goodsTypeType)\n" + 
				 		"select '"+companyIdnew+"', goodsTypeId, goodsTypeName, P_goodsTypeId, P_goodsTypeName, memo, '"+useridnew+"', now(), '"+useridnew+"', now(), isdelete, goodsTypeType\n" + 
				 		"from g_goodstype where companyId ='"+companyIdDemo+"'  ");
				this.getJdbcTemplate_newg().update(sb1.toString());
				
				
		}
		
		//插入采购订单
		 sb.delete(0, sb.length());
	   	 sb.append("select uuid, purchaseName, purchaseId, serial, purchaseState, companyId, departmentId, applyUid, applyTime, customerID, customerName, address, phone, cusname, goodsUUid, companyGoodsId, inoutNum, memo, reviewerId, reviewerTime, reviewerResult, reviewerSuggest, companyInoutNum, companyDealUserId, companyDealTime, companyResult, companyMemo, isNew, createUid, createTime, modifyUid, modifyTime, isdelete\n" + 
	   	 		"from g_purchase where companyId = '"+companyIdDemo+"'  and isdelete='0' ");
			List<Map<String, Object>> purchaseList = this.getJdbcTemplate_newg().queryForList(sb.toString(), new Object[]{});
		 for (int i=0;i<purchaseList.size();i++) {
				 String purchaseid=(String)purchaseList.get(i).get("uuid");
				 String purchaseidnew="";
				try {
					purchaseidnew = DBUtils.getDjbh(companyIdnew, "PDD");
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				//获取对应的departmentId
				 ShowData data2=new ShowData();
				data2.setTablename("t_department");
				data2.setKey("departmentId");
				data2.setDemoKeyvalue((String)purchaseList.get(i).get("departmentId"));
				data2.setDemocompanyid(companyIdDemo);
				data2.setDemouserid(useridDemo);
				String departmentIdnew = queryKeyValue(data2);
				
				//获取对应的applyUid
				 ShowData data3=new ShowData();
				 data3.setTablename("t_user");
				 data3.setKey("id");
				 data3.setDemoKeyvalue(((Long)purchaseList.get(i).get("applyUid")).toString());
				 data3.setDemocompanyid(companyIdDemo);
				 data3.setDemouserid(useridDemo);
				String applyIdnew = queryKeyValue(data3);
				if ("".equals(applyIdnew)) {
				    String applyId =((Long)purchaseList.get(i).get("applyUid")).toString();
					if(applyId.equals(useridDemo)) {
						applyIdnew = useridnew;
					}
				}
				
				//获取对应的goodsUUid
				 ShowData data4=new ShowData();
				 data4.setTablename("g_goods");
				 data4.setKey("uuid");
				 data4.setDemoKeyvalue((String)purchaseList.get(i).get("goodsUUid"));
				 data4.setDemocompanyid(companyIdDemo);
				 data4.setDemouserid(useridDemo);
				String goodsUUidnew = queryKeyValue(data4);
				
				//获取对应的reviewerId
				String reviewerId=((Long)purchaseList.get(i).get("reviewerId")).toString();
				String reviewerIdnew="";
				if (reviewerId!=null || !"".equals(reviewerId)) {
				 ShowData data5=new ShowData();
				 data5.setTablename("t_user");
				 data5.setKey("id");
				 data5.setDemoKeyvalue(reviewerId);
				 data5.setDemocompanyid(companyIdDemo);
				 data5.setDemouserid(useridDemo);
				 reviewerIdnew = queryKeyValue(data5);
				}
				
				 sb1.delete(0, sb1.length());
				 UUID uuID = UUID.randomUUID();
				 String purchaseuuidnew = uuID.toString();
				
				 sb1.append("INSERT INTO g_purchase (uuid, purchaseName, purchaseId, serial, purchaseState, companyId, departmentId, applyUid, applyTime, customerID, customerName, address, phone, cusname, goodsUUid, companyGoodsId, inoutNum, memo, reviewerId, reviewerTime, reviewerResult, reviewerSuggest, companyInoutNum, companyDealUserId, companyDealTime, companyResult, companyMemo, isNew, createUid, createTime, modifyUid, modifyTime, isdelete) \n" + 
				 		"select '"+purchaseuuidnew+"', purchaseName, '"+purchaseidnew+"', serial, purchaseState, '"+companyIdnew+"', '"+departmentIdnew+"', if('"+applyIdnew+"'='',null,CONVERT('"+applyIdnew+"',unsigned)), applyTime, customerID, customerName, address, phone, cusname,  '"+goodsUUidnew+"', companyGoodsId, inoutNum, memo,  if('"+reviewerIdnew+"'='',null,CONVERT('"+reviewerIdnew+"',unsigned)) , reviewerTime, reviewerResult, reviewerSuggest, companyInoutNum, companyDealUserId, companyDealTime, companyResult, companyMemo, isNew, createUid, createTime, modifyUid, modifyTime, isdelete\n" + 
				 		"from g_purchase where companyId= '"+companyIdDemo+"' and uuid='"+purchaseid+"' ");
				this.getJdbcTemplate_newg().update(sb1.toString());
				
				//插入到记录表
				ShowData data=new ShowData();
				data.setTablename("g_purchase");
				data.setKey("purchaseId");
				data.setKeyvalue(purchaseuuidnew);
				data.setDemoKeyvalue(purchaseid);
				data.setCompanyid(companyIdnew);
				data.setUserid(useridnew);
				data.setDemocompanyid(companyIdDemo);
				data.setDemouserid(useridDemo);
				data.setKeytype("mainkey");
				insertDemodata(data);
				
				
		}
		 
		//插入入库申请g_order_in_apply
		 sb.delete(0, sb.length());
	   	 sb.append("select uuid, applyId, purchaseId, serial, orderType, orderState, companyId, departmentId, applyUid, applyTime, customerID, customerName, address, phone, cusname, goodsUUid, inoutNum, memo, reviewerId, reviewerTime, reviewerResult, reviewerSuggest, isdelete\n" + 
	   	 		"FROM g_order_in_apply where companyId =  '"+companyIdDemo+"'  and isdelete='0' ");
			List<Map<String, Object>> orderInApplyList = this.getJdbcTemplate_newg().queryForList(sb.toString(), new Object[]{});
		 for (int i=0;i<orderInApplyList.size();i++) {
				 String orderInApplyid=(String)orderInApplyList.get(i).get("applyId");
				 String orderInApplyidnew="";
				try {
					orderInApplyidnew = DBUtils.getDjbh(companyIdnew, "SQD");
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				//获取对应的purchaseId
				 ShowData data1=new ShowData();
				data1.setTablename("g_purchase");
				data1.setKey("purchaseId");
				data1.setDemoKeyvalue((String)orderInApplyList.get(i).get("customerId"));
				data1.setDemocompanyid(companyIdDemo);
				data1.setDemouserid(useridDemo);
				String purchaseIdnew = queryKeyValue(data1);
				
				//获取对应的departmentId
				 ShowData data2=new ShowData();
				data2.setTablename("t_department");
				data2.setKey("departmentId");
				data2.setDemoKeyvalue((String)orderInApplyList.get(i).get("departmentId"));
				data2.setDemocompanyid(companyIdDemo);
				data2.setDemouserid(useridDemo);
				String departmentIdnew = queryKeyValue(data2);
				
				//获取对应的applyUid
				 ShowData data3=new ShowData();
				 data3.setTablename("t_user");
				 data3.setKey("id");
				 data3.setDemoKeyvalue(((Long)orderInApplyList.get(i).get("applyUid")).toString());
				 data3.setDemocompanyid(companyIdDemo);
				 data3.setDemouserid(useridDemo);
				String applyIdnew = queryKeyValue(data3);
				if ("".equals(applyIdnew)) {
					String applyId =((Long)orderInApplyList.get(i).get("applyUid")).toString();
					if(applyId.equals(useridDemo)) {
						applyIdnew = useridnew;
					}
				}
				
				//获取对应的goodsUUid
				 ShowData data4=new ShowData();
				 data4.setTablename("g_goods");
				 data4.setKey("uuid");
				 data4.setDemoKeyvalue((String)orderInApplyList.get(i).get("goodsUUid"));
				 data4.setDemocompanyid(companyIdDemo);
				 data4.setDemouserid(useridDemo);
				String goodsUUidnew = queryKeyValue(data4);
				
				//获取对应的reviewerId
				String reviewerIdnew ="";
				if((Long)orderInApplyList.get(i).get("reviewerId")!=null) {
				 ShowData data5=new ShowData();
				 data5.setTablename("t_user");
				 data5.setKey("id");
				 data5.setDemoKeyvalue(((Long)orderInApplyList.get(i).get("reviewerId")).toString());
				 data5.setDemocompanyid(companyIdDemo);
				 data5.setDemouserid(useridDemo);
				 reviewerIdnew = queryKeyValue(data5);
				
				}
				
				 sb1.delete(0, sb1.length());
			     String uuID = UUID.randomUUID().toString();
				 sb1.append("INSERT INTO g_order_in_apply (uuid, applyId, purchaseId, serial, orderType, orderState, companyId, departmentId, applyUid, applyTime, customerID, customerName, address, phone, cusname, goodsUUid, inoutNum, memo, reviewerId, reviewerTime, reviewerResult, reviewerSuggest, isdelete)\n" + 
				 		"select '"+uuID+"', '"+orderInApplyidnew+"', '"+purchaseIdnew+"', serial, orderType, orderState, '"+companyIdnew+"', '"+departmentIdnew+"',  IF ('"+applyIdnew+"' = '', NULL, CAST('"+applyIdnew+"' AS unsigned)) , applyTime, customerID, customerName, address, phone, cusname, '"+goodsUUidnew+"', inoutNum, memo,  IF ('"+reviewerIdnew+"' = '', NULL, CAST('"+reviewerIdnew+"' AS unsigned)) , reviewerTime, reviewerResult, reviewerSuggest, isdelete\n" + 
				 		"FROM g_order_in_apply where companyId = '"+companyIdDemo+"' and applyId='"+orderInApplyid+"' ");
				this.getJdbcTemplate_newg().update(sb1.toString());
				
				//插入到记录表
				ShowData data=new ShowData();
				data.setTablename("g_order_in_apply");
				data.setKey("applyId");
				data.setKeyvalue(orderInApplyidnew);
				data.setDemoKeyvalue(orderInApplyid);
				data.setCompanyid(companyIdnew);
				data.setUserid(useridnew);
				data.setDemocompanyid(companyIdDemo);
				data.setDemouserid(useridDemo);
				data.setKeytype("mainkey");
				insertDemodata(data);
		}
		 
		//插入仓库g_warehouse
		 sb.delete(0, sb.length());
	   	 sb.append("SELECT id, companyId, warehouseId, warehouseName, P_warehouseId, P_warehouseName, memo, createUid, createTime, modifyUid, modifyTime, isdelete\n" + 
	   	 		"from g_warehouse WHERE companyId='"+companyIdDemo+"'  and isdelete='0'  ORDER by warehouseId ");
			List<Map<String, Object>> warehouseList = this.getJdbcTemplate_newg().queryForList(sb.toString(), new Object[]{});
		 for (int i=0;i<warehouseList.size();i++) {
				 String warehouseId=(String)warehouseList.get(i).get("warehouseId");
				 String P_warehouseId=(String)warehouseList.get(i).get("P_warehouseId");
				 String warehouseIdnew ="";
				 String P_warehouseIdnew ="";
				 if("0000".equals(P_warehouseId)) {
					 P_warehouseIdnew="0000";
					 warehouseIdnew = goodsDaoImpl.getNextWarehouseTypeId(P_warehouseIdnew);
				 }else {
					//获取对应的reviewerId
					 ShowData data5=new ShowData();
					 data5.setTablename("g_warehouse");
					 data5.setKey("warehouseId");
					 data5.setDemoKeyvalue(P_warehouseId);
					 data5.setDemocompanyid(companyIdDemo);
					 data5.setDemouserid(useridDemo);
					 P_warehouseIdnew = queryKeyValue(data5);
					 warehouseIdnew = goodsDaoImpl.getNextWarehouseTypeId(P_warehouseIdnew);
					 
				 }
				
			
				 sb1.delete(0, sb1.length());
			     String uuID = UUID.randomUUID().toString();
				
				 sb1.append("INSERT INTO g_warehouse (companyId, warehouseId, warehouseName, P_warehouseId, P_warehouseName, memo, createUid, createTime, modifyUid, modifyTime, isdelete)\n" + 
				 		"SELECT  '"+companyIdnew+"', '"+warehouseIdnew+"', warehouseName, '"+P_warehouseIdnew+"', P_warehouseName, memo, createUid, createTime, modifyUid, modifyTime, isdelete\n" + 
				 		"from g_warehouse WHERE companyId='"+companyIdDemo+"' and warehouseId='"+warehouseId+"' ");
				this.getJdbcTemplate_newg().update(sb1.toString());
				
				//插入到记录表
				ShowData data=new ShowData();
				data.setTablename("g_warehouse");
				data.setKey("warehouseId");
				data.setKeyvalue(warehouseIdnew);
				data.setDemoKeyvalue(warehouseId);
				data.setCompanyid(companyIdnew);
				data.setUserid(useridnew);
				data.setDemocompanyid(companyIdDemo);
				data.setDemouserid(useridDemo);
				data.setKeytype("mainkey");
				insertDemodata(data);
		}
		 
		//插入入库单g_order_in
		 sb.delete(0, sb.length());
	   	 sb.append("select uuid, orderId, applyId, serial, orderType, companyId, goodsUUid, inoutNum, memo, warehouseId, warehouseName, warehouseKeeperID, warehouseTime, returnUuid, isReturn, isdelete\n" + 
	   	 		"from g_order_in where companyId= '"+companyIdDemo+"'   and isdelete='0' ");
			List<Map<String, Object>> orderInList = this.getJdbcTemplate_newg().queryForList(sb.toString(), new Object[]{});
		 for (int i=0;i<orderInList.size();i++) {
				 String orderInid=(String)orderInList.get(i).get("orderId");
				 String orderInidnew="";
				try {
					orderInidnew = DBUtils.getDjbh(companyIdnew, "RKD");
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				//获取对应的applyId
				 ShowData data1=new ShowData();
				data1.setTablename("g_order_in_apply");
				data1.setKey("applyId");
				data1.setDemoKeyvalue((String)orderInList.get(i).get("applyId"));
				data1.setDemocompanyid(companyIdDemo);
				data1.setDemouserid(useridDemo);
				String applyIdnew = queryKeyValue(data1);
				
				//获取对应的goodsUUid
				 ShowData data4=new ShowData();
				 data4.setTablename("g_goods");
				 data4.setKey("uuid");
				 data4.setDemoKeyvalue((String)orderInList.get(i).get("goodsUUid"));
				 data4.setDemocompanyid(companyIdDemo);
				 data4.setDemouserid(useridDemo);
				String goodsUUidnew = queryKeyValue(data4);
				
				//获取对应的warehouseKeeperID
				 ShowData data5=new ShowData();
				 data5.setTablename("t_user");
				 data5.setKey("id");
				 data5.setDemoKeyvalue((String)orderInList.get(i).get("warehouseKeeperID"));
				 data5.setDemocompanyid(companyIdDemo);
				 data5.setDemouserid(useridDemo);
				String warehouseKeeperIDnew = queryKeyValue(data5);
				
				//获取对应的warehouseId
				 ShowData data6=new ShowData();
				 data6.setTablename("g_warehouse");
				 data6.setKey("warehouseId");
				 data6.setDemoKeyvalue((String)orderInList.get(i).get("warehouseId"));
				 data6.setDemocompanyid(companyIdDemo);
				 data6.setDemouserid(useridDemo);
				String warehouseIdnew = queryKeyValue(data6);
				
				 sb1.delete(0, sb1.length());
			     String uuID = UUID.randomUUID().toString();
				  sb1.append("INSERT INTO g_order_in (uuid, orderId, applyId, serial, orderType, companyId, goodsUUid, inoutNum, memo, warehouseId, warehouseName, warehouseKeeperID, warehouseTime, returnUuid, isReturn, isdelete) \n" + 
				 		"select uuid(), '"+orderInidnew+"', '"+applyIdnew+"', serial, orderType, '"+companyIdnew+"', '"+goodsUUidnew+"', inoutNum, memo, '"+warehouseIdnew+"', warehouseName, '"+warehouseKeeperIDnew+"', warehouseTime, returnUuid, isReturn, isdelete\n" + 
				 		"from g_order_in where companyId='"+companyIdDemo+"' and orderId='"+orderInid+"' ");
				this.getJdbcTemplate_newg().update(sb1.toString());
				
				//插入到记录表
				ShowData data=new ShowData();
				data.setTablename("g_order_in");
				data.setKey("orderId");
				data.setKeyvalue(orderInidnew);
				data.setDemoKeyvalue(orderInid);
				data.setCompanyid(companyIdnew);
				data.setUserid(useridnew);
				data.setDemocompanyid(companyIdDemo);
				data.setDemouserid(useridDemo);
				data.setKeytype("mainkey");
				insertDemodata(data);
				
		}
			
		 //插入出库申请g_order_out_apply
		 sb.delete(0, sb.length());
	   	 sb.append("select uuid, applyId, purchaseId, serial, orderType, orderState, companyId, departmentId, applyUid, applyTime, customerID, customerName, address, phone, cusname, goodsUUid, inoutNum, memo, reviewerId, reviewerTime, reviewerResult, reviewerSuggest, isdelete\n" + 
	   	 		"from g_order_out_apply where companyId=  '"+companyIdDemo+"'  and isdelete='0' ");
			List<Map<String, Object>> orderOutApplyList = this.getJdbcTemplate_newg().queryForList(sb.toString(), new Object[]{});
		 for (int i=0;i<orderOutApplyList.size();i++) {
				 String orderOutApplyid=(String)orderOutApplyList.get(i).get("applyId");
				 String orderOutApplyidnew="";
				try {
					orderOutApplyidnew = DBUtils.getDjbh(companyIdnew, "SQD");
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				//获取对应的purchaseId
				 ShowData data1=new ShowData();
				data1.setTablename("g_purchase");
				data1.setKey("purchaseId");
				data1.setDemoKeyvalue((String)orderOutApplyList.get(i).get("customerId"));
				data1.setDemocompanyid(companyIdDemo);
				data1.setDemouserid(useridDemo);
				String purchaseIdnew = queryKeyValue(data1);
				
				//获取对应的departmentId
				 ShowData data2=new ShowData();
				data2.setTablename("t_department");
				data2.setKey("departmentId");
				data2.setDemoKeyvalue((String)orderOutApplyList.get(i).get("departmentId"));
				data2.setDemocompanyid(companyIdDemo);
				data2.setDemouserid(useridDemo);
				String departmentIdnew = queryKeyValue(data2);
				
				//获取对应的applyUid
				 ShowData data3=new ShowData();
				 data3.setTablename("t_user");
				 data3.setKey("id");
				 data3.setDemoKeyvalue(((Long)orderOutApplyList.get(i).get("applyUid")).toString());
				 data3.setDemocompanyid(companyIdDemo);
				 data3.setDemouserid(useridDemo);
				String applyIdnew = queryKeyValue(data3);
				if ("".equals(applyIdnew)) {
					String reviewerId =((Long)orderOutApplyList.get(i).get("applyUid")).toString();
					if(reviewerId.equals(useridDemo)) {
						applyIdnew = useridnew;
					}
				}
			
				
				//获取对应的goodsUUid
				 ShowData data4=new ShowData();
				 data4.setTablename("g_goods");
				 data4.setKey("uuid");
				 data4.setDemoKeyvalue((String)orderOutApplyList.get(i).get("goodsUUid"));
				 data4.setDemocompanyid(companyIdDemo);
				 data4.setDemouserid(useridDemo);
				String goodsUUidnew = queryKeyValue(data4);
				
				//获取对应的reviewerId
				String reviewerId = String.valueOf((Long)orderOutApplyList.get(i).get("reviewerId"));
				String  reviewerIdnew="";
				if(reviewerId!=null || !"".equals(reviewerId)) {
				 ShowData data5=new ShowData();
				 data5.setTablename("t_user");
				 data5.setKey("id");
				 data5.setDemoKeyvalue(reviewerId);
				 data5.setDemocompanyid(companyIdDemo);
				 data5.setDemouserid(useridDemo);
				 reviewerIdnew = queryKeyValue(data5);
				}
				
				
				 sb1.delete(0, sb1.length());
			     String uuID = UUID.randomUUID().toString();
				 sb1.append("INSERT INTO g_order_out_apply (uuid, applyId, purchaseId, serial, orderType, orderState, companyId, departmentId, applyUid, applyTime, customerID, customerName, address, phone, cusname, goodsUUid, inoutNum, memo, reviewerId, reviewerTime, reviewerResult, reviewerSuggest, isdelete)\n" + 
				 		"select '"+uuID+"', '"+orderOutApplyidnew+"', '"+purchaseIdnew+"', serial, orderType, orderState, '"+companyIdnew+"', '"+departmentIdnew+"', '"+applyIdnew+"', applyTime, customerID, customerName, address, phone, cusname, '"+goodsUUidnew+"', inoutNum, memo,  if('"+reviewerIdnew+"'='',null,CONVERT('"+reviewerIdnew+"',unsigned)), reviewerTime, reviewerResult, reviewerSuggest, isdelete\n" + 
				 		"FROM g_order_out_apply where companyId = '"+companyIdDemo+"' and applyId='"+orderOutApplyid+"' ");
				 this.getJdbcTemplate_newg().update(sb1.toString());
				
				//插入到记录表
				ShowData data=new ShowData();
				data.setTablename("g_order_out_apply");
				data.setKey("applyId");
				data.setKeyvalue(orderOutApplyidnew);
				data.setDemoKeyvalue(orderOutApplyid);
				data.setCompanyid(companyIdnew);
				data.setUserid(useridnew);
				data.setDemocompanyid(companyIdDemo);
				data.setDemouserid(useridDemo);
				data.setKeytype("mainkey");
				insertDemodata(data);
		} 
		
		 //插入出库单g_order_out
		 sb.delete(0, sb.length());
	   	 sb.append("select uuid, orderId, applyId, serial, orderType, companyId, goodsUUid, inoutNum, memo, warehouseId, warehouseName, warehouseKeeperID, warehouseTime, returnUuid, isReturn, isdelete\n" + 
	   	 		"from g_order_out where companyId= '"+companyIdDemo+"'  and isdelete='0' ");
			List<Map<String, Object>> orderOutList = this.getJdbcTemplate_newg().queryForList(sb.toString(), new Object[]{});
		 for (int i=0;i<orderOutList.size();i++) {
				 String id=(String)orderOutList.get(i).get("orderId");
				 String idnew="";
				try {
					idnew = DBUtils.getDjbh(companyIdnew, "CKD");
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				//获取对应的applyId
				 ShowData data1=new ShowData();
				data1.setTablename("g_order_in_apply");
				data1.setKey("applyId");
				data1.setDemoKeyvalue((String)orderOutList.get(i).get("applyId"));
				data1.setDemocompanyid(companyIdDemo);
				data1.setDemouserid(useridDemo);
				String applyIdnew = queryKeyValue(data1);
				
				//获取对应的goodsUUid
				 ShowData data4=new ShowData();
				 data4.setTablename("g_goods");
				 data4.setKey("uuid");
				 data4.setDemoKeyvalue((String)orderOutList.get(i).get("goodsUUid"));
				 data4.setDemocompanyid(companyIdDemo);
				 data4.setDemouserid(useridDemo);
				String goodsUUidnew = queryKeyValue(data4);
				
				//获取对应的warehouseKeeperID
				 ShowData data5=new ShowData();
				 data5.setTablename("t_user");
				 data5.setKey("id");
				 data5.setDemoKeyvalue(((Long)orderOutList.get(i).get("warehouseKeeperID")).toString());
				 data5.setDemocompanyid(companyIdDemo);
				 data5.setDemouserid(useridDemo);
				String warehouseKeeperIDnew = queryKeyValue(data5);
				if ("".equals(warehouseKeeperIDnew)) {
					String warehouseKeeperID =((Long)orderOutList.get(i).get("warehouseKeeperID")).toString();
					if(warehouseKeeperID.equals(useridDemo)) {
						warehouseKeeperIDnew = useridnew;
					}
				}
				
				//获取对应的warehouseId
				 ShowData data6=new ShowData();
				 data6.setTablename("g_warehouse");
				 data6.setKey("warehouseId");
				 data6.setDemoKeyvalue((String)orderOutList.get(i).get("warehouseId"));
				 data6.setDemocompanyid(companyIdDemo);
				 data6.setDemouserid(useridDemo);
				String warehouseIdnew = queryKeyValue(data6);
				
				 sb1.delete(0, sb1.length());
			     String uuID = UUID.randomUUID().toString();
				 
				 sb1.append("INSERT INTO g_order_out (uuid, orderId, applyId, serial, orderType, companyId, goodsUUid, inoutNum, memo, warehouseId, warehouseName, warehouseKeeperID, warehouseTime, returnUuid, isReturn, isdelete) \n" + 
				 		"select uuid(), '"+idnew+"', '"+applyIdnew+"', serial, orderType, '"+companyIdnew+"', '"+goodsUUidnew+"', inoutNum, memo, '"+warehouseIdnew+"', warehouseName, '"+warehouseKeeperIDnew+"', warehouseTime, returnUuid, isReturn, isdelete\n" + 
				 		"from g_order_out where companyId='"+companyIdDemo+"' and orderId='"+id+"' ");
				this.getJdbcTemplate_newg().update(sb1.toString());
				
				//插入到记录表
				ShowData data=new ShowData();
				data.setTablename("g_order_out");
				data.setKey("orderId");
				data.setKeyvalue(idnew);
				data.setDemoKeyvalue(id);
				data.setCompanyid(companyIdnew);
				data.setUserid(useridnew);
				data.setDemocompanyid(companyIdDemo);
				data.setDemouserid(useridDemo);
				data.setKeytype("mainkey");
				insertDemodata(data);
				
		} 
		
		//插入工程t_project
		 sb.delete(0, sb.length());
	   	 sb.append("select id, uuid, contract_no, project_id, project_name, planned_start_time, planned_end_time, investment_scale, area_code, company_id, department_id, charge, service_mode, project_status, project_type, project_level, contact, contact_phone, project_address, latitude, longitude, remark, images, is_payment, opportunity_uuid, clue_uuid, customer_uuid, createUid, createTime, modifyUid, modifyTime, manufacture_id, ope_manufacture_id, jsondata, templateId\n" + 
	   	 		"FROM t_project WHERE company_id= '"+companyIdDemo+"' ");
			List<Map<String, Object>> projectList = this.getJdbcTemplate_newg().queryForList(sb.toString(), new Object[]{});
		 for (int i=0;i<projectList.size();i++) {
				 String id=(String)projectList.get(i).get("uuid");
				 String idnew="";
					try {
						idnew = DBUtils.getDjbh(companyIdnew, "PRO");
					} catch (SQLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					
				//获取对应的departmentId
				 ShowData data2=new ShowData();
				data2.setTablename("t_department");
				data2.setKey("departmentId");
				data2.setDemoKeyvalue((String)projectList.get(i).get("department_id"));
				data2.setDemocompanyid(companyIdDemo);
				data2.setDemouserid(useridDemo);
				String departmentIdnew = queryKeyValue(data2);
				
				//获取对应的opportunity_uuid
				 ShowData data3=new ShowData();
				 data3.setTablename("opportunity");
				 data3.setKey("uuid");
				 data3.setDemoKeyvalue((String)projectList.get(i).get("opportunity_uuid"));
				 data3.setDemocompanyid(companyIdDemo);
				 data3.setDemouserid(useridDemo);
				String opportunity_uuidnew = queryKeyValue(data3);
				
				 sb1.delete(0, sb1.length());
			     String uuID = UUID.randomUUID().toString();
				 sb1.append("INSERT INTO t_project (uuid, contract_no, project_id, project_name, planned_start_time, planned_end_time, investment_scale, area_code, company_id, department_id, charge, service_mode, project_status, project_type, project_level, contact, contact_phone, project_address, latitude, longitude, remark, images, is_payment, opportunity_uuid, clue_uuid, customer_uuid, createUid, createTime, modifyUid, modifyTime, manufacture_id, ope_manufacture_id, jsondata, templateId) \n" + 
				 		"select  '"+uuID+"', contract_no, '"+idnew+"', project_name, planned_start_time, planned_end_time, investment_scale, area_code, '"+companyIdnew+"', '"+departmentIdnew+"', charge, service_mode, project_status, project_type, project_level, contact, contact_phone, project_address, latitude, longitude, remark, images, is_payment, '"+opportunity_uuidnew+"', clue_uuid, customer_uuid, '"+useridnew+"', now(), '"+useridnew+"', now(), manufacture_id, ope_manufacture_id, jsondata, templateId\n" + 
				 		"FROM t_project WHERE company_id='"+companyIdDemo+"' and uuid='"+id+"' ");
				this.getJdbcTemplate_newg().update(sb1.toString());
				
				
				
				//插入到记录表
				ShowData data=new ShowData();
				data.setTablename("t_project");
				data.setKey("uuid");
				data.setKeyvalue(uuID);
				data.setDemoKeyvalue(id);
				data.setCompanyid(companyIdnew);
				data.setUserid(useridnew);
				data.setDemocompanyid(companyIdDemo);
				data.setDemouserid(useridDemo);
				data.setKeytype("mainkey");
				insertDemodata(data);
				
				//插入到t_project_bulid_log
				 String logNo="";
				try {
					logNo = DBUtils.getDjbh(companyIdnew, "LOG");
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				 sb1.delete(0, sb1.length());
				 sb1.append("INSERT INTO t_project_bulid_log (logNo, company_id, uuid, project_uuid, personnel, finishRate, personnelnum, weather, remark, images, createUid, createTime, modifyUid, modifyTime) \n" + 
				 		"SELECT '"+logNo+"', '"+companyIdnew+"', uuid(), '"+uuID+"', personnel, finishRate, personnelnum, weather, remark, images, '"+useridnew+"', createTime, modifyUid, modifyTime\n" + 
				 		"from t_project_bulid_log WHERE  company_id='"+companyIdDemo+"' and project_uuid='"+id+"' ");
				this.getJdbcTemplate_newg().update(sb1.toString());
				
				
				//插入到t_project_check
				sb1.delete(0, sb1.length());
				sb1.append("select uuid,check_user_id from t_project_check WHERE  project_uuid='"+id+"' ");
				List<Map<String, Object>> projectCheckList = this.getJdbcTemplate_newg().queryForList(sb1.toString(), new Object[]{});
				for (int j=0;j<projectCheckList.size();j++) {
					 String projectCheckId=(String)projectCheckList.get(j).get("uuid");
					 String check_user_id=String.valueOf((Long)projectCheckList.get(j).get("check_user_id"));
					 String check_no="";
					try {
						check_no = DBUtils.getDjbh(companyIdnew, "CHK");
					} catch (SQLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					//获取对应的check_user_id
					String check_user_idnew="";
					if (check_user_id!=null && !"".equals(check_user_id))
					{
					 ShowData data5=new ShowData();
					 data5.setTablename("t_user");
					 data5.setKey("id");
					 data5.setDemoKeyvalue(check_user_id);
					 data5.setDemocompanyid(companyIdDemo);
					 data5.setDemouserid(useridDemo);
					 check_user_idnew = queryKeyValue(data5);	
					}
				 sb2.delete(0, sb2.length());
				 sb2.append("INSERT INTO t_project_check ( check_no, uuid, project_uuid, check_type, check_user_id, check_content, check_results, images, check_status, createUid, checkTime, createTime, modifyUid, modifyTime) \n" + 
				 		"SELECT  '"+check_no+"', uuid(), '"+uuID+"', check_type, IF ('"+check_user_idnew+"' = '', NULL, CAST('"+check_user_idnew+"' AS unsigned)), check_content, check_results, images, check_status, '"+useridnew+"', checkTime, createTime, modifyUid, modifyTime\n" + 
				 		"from t_project_check where uuid='"+projectCheckId+"'  ");
				this.getJdbcTemplate_newg().update(sb2.toString()); 
				}
				
				//插入t_project_record
				sb1.delete(0, sb1.length());
				 sb1.append("INSERT INTO t_project_record (uuid, project_uuid, project_status, receiverId, remark, images, createUid, createTime, modifyUid, modifyTime) \n" + 
				 		"select uuid(), '"+uuID+"', project_status, receiverId, remark, images, '"+useridnew+"', createTime, modifyUid, modifyTime\n" + 
				 		"from t_project_record where  project_uuid='"+id+"' ");
				this.getJdbcTemplate_newg().update(sb1.toString());
				
				//插入t_project_reporting
				sb1.delete(0, sb1.length());
				 sb1.append("INSERT INTO t_project_reporting (uuid, company_id, manufacture_id, manufacture_name, project_name, part_a_name, part_a_contact, part_a_contact_phone, part_b_name, part_b_contact, part_b_contact_phone, area_code, project_address, project_type, investment_scale, build_area, planned_start_time, planned_end_time, remark, images, createUid, createTime, modifyUid, modifyTime, shzt, shyj) \n" + 
				 		"SELECT uuid(), '"+companyIdnew+"', manufacture_id, manufacture_name, project_name, part_a_name, part_a_contact, part_a_contact_phone, part_b_name, part_b_contact, part_b_contact_phone, area_code, project_address, project_type, investment_scale, build_area, planned_start_time, planned_end_time, remark, images, '"+useridnew+"', createTime, modifyUid, modifyTime, shzt, shyj\n" + 
				 		"from t_project_reporting WHERE  company_id='"+companyIdDemo+"' ");
				this.getJdbcTemplate_newg().update(sb1.toString());
				
				//插入t_project_scheduling
				sb1.delete(0, sb1.length());
				 sb1.append("INSERT INTO t_project_scheduling (uuid, project_uuid, department_id, start_time, end_time, isNew, createUid, createTime, modifyUid, modifyTime)\n" + 
				 		"SELECT uuid(), '"+uuID+"', department_id, start_time, end_time, isNew, '"+useridnew+"', createTime, modifyUid, modifyTime \n" + 
				 		"from t_project_scheduling where  project_uuid='"+id+"' ");
				this.getJdbcTemplate_newg().update(sb1.toString());
				
				//插入project_file
				sb1.delete(0, sb1.length());
				 sb1.append("INSERT INTO `project_file` (`uuid`, `projectId`, `filetype`, `filename`, `filepath`, `createTime`, `createUid`, `memo`, `isdelete`) \n" + 
				 		"	SELECT uuid(), '"+uuID+"', `filetype`, `filename`, `filepath`, `createTime`, '"+useridnew+"', `memo`, `isdelete` \n" + 
				 		"	from project_file WHERE projectId='"+id+"' ");
				this.getJdbcTemplate_newg().update(sb1.toString());
			
				
		}
		 
		//插入t_project_log
		 sb.delete(0, sb.length());
		 sb.append("INSERT INTO t_project_log ( link_id, link_type, title, sub_title, remark, createUid, createTime, modifyUid, modifyTime)\n" + 
		 		"SELECT  b.keyvalue, link_type, title, sub_title, remark, '"+useridnew+"', createTime, modifyUid, modifyTime\n" + 
		 		"from t_project_log a INNER JOIN d_demodata b on a.link_id = b.demoKeyvalue and b.democompanyid =  '"+companyIdDemo+"' ");
		this.getJdbcTemplate_newg().update(sb.toString());
		
		 //插入t_work_order_info
		 sb.delete(0, sb.length());
	   	 sb.append("select uuid, work_order_id, company_id, manufacture_id, dd_id, contact, contact_phone, work_type_id, address, xzqhdm, `describe`, maintain_id, `status`, handle_type_id, handle_desc, images, handle_mode, erro_code, handle_id, agent_id, gdlx, gddj, clsx, distance, repairType, repairPrice, baseDistance, pricePerKM, roundTrip, auditStatus, auditComments, auditTime, auditUserId, planned_start_time, planned_end_time, isReturnVisit, repair_result, attitude, charged, efficiency, remarks, rating, closingreason, createUid, createTime, modifyUid, modifyTime\n" + 
	   	 		"from t_work_order_info where company_id = '"+companyIdDemo+"' ");
			List<Map<String, Object>> workOrderInfoList = this.getJdbcTemplate_newg().queryForList(sb.toString(), new Object[]{});
		 for (int i=0;i<workOrderInfoList.size();i++) {
				 String id=(String)workOrderInfoList.get(i).get("work_order_id");
				 String dd_id=(String)workOrderInfoList.get(i).get("dd_id");
				 String uuID = UUID.randomUUID().toString();
				 String idnew="";
				try {
					idnew = DBUtils.getDjbh(companyIdnew, "WOR");
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				//获取对应的project_id
				 ShowData data5=new ShowData();
				 data5.setTablename("t_project");
				 data5.setKey("uuid");
				 data5.setDemoKeyvalue(dd_id);
				 data5.setDemocompanyid(companyIdDemo);
				 data5.setDemouserid(useridDemo);
				String dd_idnew = queryKeyValue(data5);	
				
				 sb1.delete(0, sb1.length());
				 sb1.append("INSERT INTO t_work_order_info (uuid, work_order_id, company_id, manufacture_id, dd_id, contact, contact_phone, work_type_id, address, xzqhdm, `describe`, maintain_id, `status`, handle_type_id, handle_desc, images, handle_mode, erro_code, handle_id, agent_id, gdlx, gddj, clsx, distance, repairType, repairPrice, baseDistance, pricePerKM, roundTrip, auditStatus, auditComments, auditTime, auditUserId, planned_start_time, planned_end_time, isReturnVisit, repair_result, attitude, charged, efficiency, remarks, rating, closingreason, createUid, createTime, modifyUid, modifyTime) \n" + 
				 		"select '"+dd_idnew+"', '"+idnew+"', '"+companyIdnew+"', manufacture_id, '"+dd_idnew+"', contact, contact_phone, work_type_id, address, xzqhdm, `describe`, maintain_id, `status`, handle_type_id, handle_desc, images, handle_mode, erro_code, handle_id, agent_id, gdlx, gddj, clsx, distance, repairType, repairPrice, baseDistance, pricePerKM, roundTrip, auditStatus, auditComments, auditTime, auditUserId, planned_start_time, planned_end_time, isReturnVisit, repair_result, attitude, charged, efficiency, remarks, rating, closingreason, '"+useridnew+"', createTime, modifyUid, modifyTime\n" + 
				 		"from t_work_order_info where company_id = '"+companyIdDemo+"' and work_order_id='"+id+"' ");
				this.getJdbcTemplate_newg().update(sb1.toString());
				
				//插入到记录表
				ShowData data=new ShowData();
				data.setTablename("t_work_order_info");
				data.setKey("work_order_id");
				data.setKeyvalue(idnew);
				data.setDemoKeyvalue(id);
				data.setCompanyid(companyIdnew);
				data.setUserid(useridnew);
				data.setDemocompanyid(companyIdDemo);
				data.setDemouserid(useridDemo);
				data.setKeytype("mainkey");
				insertDemodata(data);
		} 
		 
	
			
		 //插入t_work_order_record
		 sb.delete(0, sb.length());
	   	 sb.append("select  record_id, company_id, work_order_id, `describe`, maintain_id, `status`, images, fault_type_id, longitude, latitude, distance, repairType, remark, createUid, createTime, modifyUid, modifyTime\n" + 
	   	 		"from t_work_order_record where company_id= '"+companyIdDemo+"' ");
			List<Map<String, Object>> workOrderRecordList = this.getJdbcTemplate_newg().queryForList(sb.toString(), new Object[]{});
		 for (int i=0;i<workOrderRecordList.size();i++) {
				 String id=(String)workOrderRecordList.get(i).get("record_id");
				 String work_order_id=(String)workOrderRecordList.get(i).get("work_order_id");
				 String idnew="";
				
				try {
					idnew = DBUtils.getDjbh(companyIdnew, "REC");
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				//获取对应的work_order_id
				 ShowData data5=new ShowData();
				 data5.setTablename("t_work_order_info");
				 data5.setKey("work_order_id");
				 data5.setDemoKeyvalue(work_order_id);
				 data5.setDemocompanyid(companyIdDemo);
				 data5.setDemouserid(useridDemo);
				String work_order_idnew = queryKeyValue(data5);	
				
				 sb1.delete(0, sb1.length());
				 sb1.append("INSERT INTO t_work_order_record ( record_id, company_id, work_order_id, `describe`, maintain_id, `status`, images, fault_type_id, longitude, latitude, distance, repairType, remark, createUid, createTime, modifyUid, modifyTime)\n" + 
				 		"select  '"+idnew+"', '"+companyIdnew+"', '"+work_order_idnew+"', `describe`, maintain_id, `status`, images, fault_type_id, longitude, latitude, distance, repairType, remark, '"+useridnew+"', createTime, modifyUid, modifyTime\n" + 
				 		"from t_work_order_record where company_id='"+companyIdDemo+"' and work_order_id='"+id+"' ");
				this.getJdbcTemplate_newg().update(sb1.toString());
				
				//插入到记录表
				ShowData data=new ShowData();
				data.setTablename("t_work_order_record");
				data.setKey("record_id");
				data.setKeyvalue(idnew);
				data.setDemoKeyvalue(id);
				data.setCompanyid(companyIdnew);
				data.setUserid(useridnew);
				data.setDemocompanyid(companyIdDemo);
				data.setDemouserid(useridDemo);
				data.setKeytype("mainkey");
				insertDemodata(data);
		} 
		 
		//插入到active_custom
		 sb1.delete(0, sb1.length());
		 sb1.append("INSERT INTO active_custom (uuid, companyId, activeName, activePic, activeTimeStart, activeTimeEnd, url, isdelete, id) \n" + 
		 		"SELECT uuid(), '"+companyIdnew+"', activeName, activePic, activeTimeStart, activeTimeEnd, url, isdelete, id\n" + 
		 		"from active_custom where companyId='"+companyIdDemo+"' and isdelete='0' ");
		this.getJdbcTemplate_newg().update(sb1.toString());
		
		//插入到active_file
		 sb1.delete(0, sb1.length());
		 sb1.append("INSERT INTO active_file (uuid, companyId, follow, title, subtitle, url, fileType, createTime) \n" + 
		 		"select uuid(), '"+companyIdnew+"', follow, title, subtitle, url, fileType, createTime\n" + 
		 		"from active_file where companyId='"+companyIdDemo+"' ");
		this.getJdbcTemplate_newg().update(sb1.toString());
		
	
		
		//插入到active_session
		 sb.delete(0, sb.length());
	   	 sb.append("select uuid, companyId, activeName, activeTimeStart, activeTimeEnd, activeCjcs, activePic, titleUrl, backgroundUrl, activeRule, mineFormMesg, indexTemplate, sfmr, activityUuid, createTime, id, forward, maxForward, sffb  "  + 
	   	 		"from active_session where companyId ='"+companyIdDemo+"' ");
			List<Map<String, Object>> sessionList = this.getJdbcTemplate_newg().queryForList(sb.toString(), new Object[]{});
		 for (int i=0;i<sessionList.size();i++) {
				 String id=(String)sessionList.get(i).get("uuid");
				 String idnew = UUID.randomUUID().toString();
				sb1.delete(0, sb1.length());
				 sb1.append("INSERT INTO active_session (uuid, companyId, activeName, activeTimeStart, activeTimeEnd, activeCjcs, activePic, titleUrl, backgroundUrl, activeRule, mineFormMesg, indexTemplate, sfmr, activityUuid, createTime,  forward, maxForward, sffb) \n" + 
				 		"select '"+idnew+"', '"+companyIdnew+"', activeName, activeTimeStart, activeTimeEnd, activeCjcs, activePic, titleUrl, backgroundUrl, activeRule, mineFormMesg, indexTemplate, sfmr, activityUuid, now(), forward, maxForward, sffb\n" + 
				 		"from active_session where companyId = '"+companyIdDemo+"' ");
				this.getJdbcTemplate_newg().update(sb1.toString());
				
				//插入到记录表
				ShowData data=new ShowData();
				data.setTablename("active_session");
				data.setKey("uuid");
				data.setKeyvalue(idnew);
				data.setDemoKeyvalue(id);
				data.setCompanyid(companyIdnew);
				data.setUserid(useridnew);
				data.setDemocompanyid(companyIdDemo);
				data.setDemouserid(useridDemo);
				data.setKeytype("mainkey");
				insertDemodata(data);
				
				//active_record
				sb1.delete(0, sb1.length());
				 sb1.append("INSERT INTO active_record (uuid, companyId, activeUuid, activeName, userUuid, userName, userPhone, numberhome, prizeName, prizeRule, sfwjp, isPay, address, createTime, prizeType, isShoppingMalls, couponId, couponName)\n" + 
				 		" SELECT uuid(), '"+companyIdnew+"', '"+idnew+"', activeName, userUuid, userName, userPhone, numberhome, prizeName, prizeRule, sfwjp, isPay, address, createTime, prizeType, isShoppingMalls, couponId, couponName\n" + 
				 		" from active_record where companyId= '"+companyIdDemo+"' and  activeUuid ='"+id+"'");
				this.getJdbcTemplate_newg().update(sb1.toString());
				
				//pc_active
				sb1.delete(0, sb1.length());
				 sb1.append("INSERT INTO `pc_active` (`uuid`, `activeTypeId`, `name`, `companyId`, `memo`, `createUid`, `createTime`, `modifyTime`, `isdelete`) \n" + 
				 		"select uuid(), '"+idnew+"', `name`, '"+companyIdnew+"', `memo`, '"+useridnew+"', now(), now(), `isdelete`\n" + 
				 		"from pc_active where  companyId= '"+companyIdDemo+"' and  activeTypeId ='"+id+"'");
				this.getJdbcTemplate_newg().update(sb1.toString());
					
		} 
		 
		//插入到active_user
		 sb.delete(0, sb.length());
		 sb.append("SELECT uuid, companyId, name, userPic, phone, companyName, businessDirection, follow, address, openid, activityUuid, jsondata, createTime, yhlb\n" + 
			 		"from active_user WHERE companyId = '"+companyIdDemo+"' ");
			List<Map<String, Object>> active_userList = this.getJdbcTemplate_newg().queryForList(sb.toString(), new Object[]{});
		 for (int i=0;i<active_userList.size();i++) {
				 String id=(String)active_userList.get(i).get("uuid");
				 String idnew = UUID.randomUUID().toString();
				 sb1.delete(0, sb1.length());
				 sb1.append("INSERT INTO active_user (uuid, companyId, name, userPic, phone, companyName, businessDirection, follow, address, openid, activityUuid, jsondata, createTime, yhlb) \n" + 
				 		"SELECT '"+idnew+"', '"+companyIdnew+"', name, userPic, phone, companyName, businessDirection, follow, address, openid, activityUuid, jsondata, createTime, yhlb\n" + 
				 		"from active_user WHERE companyId = '"+companyIdDemo+"' ");
				this.getJdbcTemplate_newg().update(sb1.toString());
				
				//插入到记录表
				ShowData data=new ShowData();
				data.setTablename("active_user");
				data.setKey("uuid");
				data.setKeyvalue(idnew);
				data.setDemoKeyvalue(id);
				data.setCompanyid(companyIdnew);
				data.setUserid(useridnew);
				data.setDemocompanyid(companyIdDemo);
				data.setDemouserid(useridDemo);
				data.setKeytype("mainkey");
				insertDemodata(data);
				
				//active_user_address
				sb1.delete(0, sb1.length());
				 sb1.append("INSERT INTO active_user_address (uuid, activeUserId, xzqh, address, name, phone, isDefault, createTime, modifyTime, isdelete) \n" + 
				 		"SELECT uuid(), '"+id+"', xzqh, address, name, phone, isDefault, createTime, modifyTime, isdelete\n" + 
				 		"from active_user_address where  activeUuid ='"+id+"'");
				this.getJdbcTemplate_newg().update(sb1.toString());
				
		}
		 
		 
			//插入到pc_active_reward
		 sb.delete(0, sb.length());
		 sb.append("select `uuid`,  `activeId` " + 
		 		" from pc_active_reward where  companyId = '"+companyIdDemo+"' ");
			List<Map<String, Object>> pc_active_rewardList = this.getJdbcTemplate_newg().queryForList(sb.toString(), new Object[]{});
		 for (int i=0;i<pc_active_rewardList.size();i++) {
				 String id=(String)pc_active_rewardList.get(i).get("uuid");
				 String activeId=(String)pc_active_rewardList.get(i).get("activeId");
				 String idnew = UUID.randomUUID().toString();
				 
				//获取对应的activeId
				 ShowData data5=new ShowData();
				 data5.setTablename("active_session");
				 data5.setKey("uuid");
				 data5.setDemoKeyvalue(activeId);
				 data5.setDemocompanyid(companyIdDemo);
				 data5.setDemouserid(useridDemo);
				String activeIdnew = queryKeyValue(data5);
				 
				 sb1.delete(0, sb1.length());
				 sb1.append("INSERT INTO `pc_active_reward` (`uuid`,  `name`, `chooseNumber`, `prize`, `prizeImg`, `activeId`, `companyId`, `createUid`, `createTime`, `modifyTime`, `isdelete`)\n" + 
					 		"	select '"+idnew+"',  `name`, `chooseNumber`, `prize`, `prizeImg`, '"+activeIdnew+"', '"+companyIdnew+"', '"+useridnew+"', now(),now(), `isdelete` \n" + 
					 		"	from pc_active_reward where   companyId= '"+companyIdDemo+"' and  uuid ='"+id+"'");
				this.getJdbcTemplate_newg().update(sb1.toString());
				
				//插入到记录表
				ShowData data=new ShowData();
				data.setTablename("pc_active_reward");
				data.setKey("uuid");
				data.setKeyvalue(idnew);
				data.setDemoKeyvalue(id);
				data.setCompanyid(companyIdnew);
				data.setUserid(useridnew);
				data.setDemocompanyid(companyIdDemo);
				data.setDemouserid(useridDemo);
				data.setKeytype("mainkey");
				insertDemodata(data);
				
		}
		 //pc_active_award_record
		 sb.delete(0, sb.length());
		 sb.append("INSERT INTO `pc_active_award_record` (`uuid`, `activeId`, `rewardId`, `clueId`, `companyId`, `isPay`, `createTime`, `isdelete`)\n" + 
		 		" SELECT uuid(), c.keyvalue, d.keyvalue, b.keyvalue, '"+companyIdnew+"', `isPay`, now(), `isdelete`\n" + 
		 		" from pc_active_award_record a " + 
		 		"left JOIN d_demodata b on a.clueId = b.demoKeyvalue and b.tablename='clue' and  b.`key`= 'uuid' and  b.democompanyid = '"+companyIdDemo+"' and b.demouserid ='"+useridDemo+"'\n" + 
		 		"left JOIN d_demodata c on a.activeId = c.demoKeyvalue and c.tablename='active_session' and  c.`key`= 'uuid' and  c.democompanyid = '"+companyIdDemo+"' and c.demouserid ='"+useridDemo+"'\n" + 
		 		"left JOIN d_demodata d on a.rewardId = d.demoKeyvalue and d.tablename='pc_active_reward' and  d.`key`= 'uuid' and  d.democompanyid = '"+companyIdDemo+"' and d.demouserid ='"+useridDemo+"'\n" + 
		 		"where  a.companyId= '"+companyIdDemo+"' ");
		this.getJdbcTemplate_newg().update(sb.toString());
		
		//pc_active_lucky_user
		 sb.delete(0, sb.length());
		 sb.append("INSERT INTO `pc_active_lucky_user` (`uuid`, `rewardId`, `activeId`, `clueId`, `companyId`, `createTime`, `isdelete`)\n" + 
		 		"SELECT  uuid(), d.keyvalue, c.keyvalue, b.keyvalue,'"+companyIdnew+"', now(), `isdelete` \n" + 
		 		"from pc_active_lucky_user a " + 
		 		"left JOIN d_demodata b on a.clueId = b.demoKeyvalue and b.tablename='clue' and  b.`key`= 'uuid' and  b.democompanyid = '"+companyIdDemo+"' and b.demouserid ='"+useridDemo+"'\n" + 
		 		"left JOIN d_demodata c on a.activeId = c.demoKeyvalue and c.tablename='active_session' and  c.`key`= 'uuid' and  c.democompanyid = '"+companyIdDemo+"' and c.demouserid ='"+useridDemo+"'\n" + 
		 		"left JOIN d_demodata d on a.rewardId = d.demoKeyvalue and d.tablename='pc_active_reward' and  d.`key`= 'uuid' and  d.democompanyid = '"+companyIdDemo+"' and d.demouserid ='"+useridDemo+"'\n" + 
		 		"where  a.companyId= '"+companyIdDemo+"' ");
		this.getJdbcTemplate_newg().update(sb.toString());
		
		
		//插入到t_activity
		 sb.delete(0, sb.length());
	   	 sb.append("select `uuid` " + 
	   	 		"from t_activity where  companyId ='"+companyIdDemo+"' ");
			List<Map<String, Object>> t_activityList = this.getJdbcTemplate_newg().queryForList(sb.toString(), new Object[]{});
		 for (int i=0;i<t_activityList.size();i++) {
				 String id=(String)t_activityList.get(i).get("uuid");
				 String idnew = UUID.randomUUID().toString();
				sb1.delete(0, sb1.length());
				 sb1.append("INSERT INTO `t_activity` (`uuid`, `name`, `cluesource`, `subname`, `starttime`, `endtime`, `companyId`, `jsondata`, `imageBg`, `imgtype`, `images`, `createUid`, `createTime`, `modifyUid`, `modifyTime`, `isdelete`, `modeid`, `isclue`, `sfmr`, `sfytbxx`, `colorTextObj`, `colorOkBtnObj`, `afterAddingImage`, `sfgsmb`, `url`, `companyrule`) \n" + 
				 		"select '"+idnew+"', `name`, `cluesource`, `subname`, `starttime`, `endtime`, '"+companyIdnew+"', `jsondata`, `imageBg`, `imgtype`, `images`, '"+useridnew+"', now(),'"+useridnew+"', now(), `isdelete`, `modeid`, `isclue`, `sfmr`, `sfytbxx`, `colorTextObj`, `colorOkBtnObj`, `afterAddingImage`, `sfgsmb`, `url`, `companyrule`\n" + 
				 		"from t_activity where  companyId = '"+companyIdDemo+"' and uuid ='"+id+"' ");
				this.getJdbcTemplate_newg().update(sb1.toString());
				
				//插入到记录表
				ShowData data=new ShowData();
				data.setTablename("t_activity");
				data.setKey("uuid");
				data.setKeyvalue(idnew);
				data.setDemoKeyvalue(id);
				data.setCompanyid(companyIdnew);
				data.setUserid(useridnew);
				data.setDemocompanyid(companyIdDemo);
				data.setDemouserid(useridDemo);
				data.setKeytype("mainkey");
				insertDemodata(data);
				
				//t_activity_collect
				sb1.delete(0, sb1.length());
				 sb1.append("INSERT INTO `t_activity_collect` (`uuid`, `activeId`, `phone`, `createTime`)\n" + 
				 		"SELECT uuid(), '"+idnew+"', `phone`, now()\n" + 
				 		"from  t_activity_collect where  activeId= '"+id+"'");
				this.getJdbcTemplate_newg().update(sb1.toString());
					
		} 
		
		
		//插入日志log_claim
		 sb.delete(0, sb.length());
		 sb.append("select `uuid`, `clueId`, `customerId`, `opportunityId`, `projectId`, `type`, `userid`, `username`, `visittype`, `visitaddress`, `visittime`, `visitcontent`, `companyId`, `imagelist`, `isdelete`, `createUid`, `createTime`, `modifyUid`, `modifyTime`\n" + 
		 		"from log_claim where  companyId= '"+companyIdDemo+"' ");
			List<Map<String, Object>> claimList = this.getJdbcTemplate_newg().queryForList(sb.toString(), new Object[]{});
		 for (int i=0;i<claimList.size();i++) {
				 String id=(String)claimList.get(i).get("uuid");
				 String idnew = UUID.randomUUID().toString();
				 sb1.delete(0, sb1.length());
				 sb1.append("INSERT `log_claim` (`uuid`, `clueId`, `customerId`, `opportunityId`, `projectId`, `type`, `userid`, `username`, `visittype`, `visitaddress`, `visittime`, `visitcontent`, `companyId`, `imagelist`, `isdelete`, `createUid`, `createTime`, `modifyUid`, `modifyTime`) \n" + 
				 		"select '"+idnew+"', b.keyvalue, c.keyvalue, d.keyvalue, e.keyvalue, `type`, if(f.keyvalue is null or f.keyvalue='' ,'"+useridnew+"',f.keyvalue), if(g.keyvalue is null or g.keyvalue='','"+usernamenew+"',g.keyvalue), `visittype`, `visitaddress`, `visittime`, `visitcontent`, '"+companyIdnew+"', `imagelist`, `isdelete`, `createUid`, `createTime`, `modifyUid`, `modifyTime`\n" + 
				 		"from log_claim a\n" + 
				 		"left JOIN d_demodata b on a.clueId = b.demoKeyvalue and b.tablename='clue' and  b.`key`= 'uuid' and  b.democompanyid = '"+companyIdDemo+"' and b.demouserid ='"+useridDemo+"'\n" + 
				 		"left JOIN d_demodata c on a.customerId = c.demoKeyvalue and c.tablename='customer' and  c.`key`= 'uuid' and  c.democompanyid = '"+companyIdDemo+"' and c.demouserid ='"+useridDemo+"'\n" + 
				 		"left JOIN d_demodata d on a.opportunityId = d.demoKeyvalue and d.tablename='opportunity' and  d.`key`= 'uuid' and  d.democompanyid = '"+companyIdDemo+"' and d.demouserid ='"+useridDemo+"'\n" + 
				 		"left JOIN d_demodata e on a.projectId = e.demoKeyvalue and e.tablename='t_project' and  e.`key`= 'uuid' and  e.democompanyid = '"+companyIdDemo+"' and e.demouserid ='"+useridDemo+"'\n" + 
				 		"left JOIN d_demodata f on a.userid = f.demoKeyvalue and f.tablename='t_user' and  f.`key`= 'id' and  f.democompanyid = '"+companyIdDemo+"' and f.demouserid ='"+useridDemo+"'\n" + 
				 		"left JOIN d_demodata g on a.username = g.demoKeyvalue and g.tablename='t_user' and  g.`key`= 'username' and  g.democompanyid = '"+companyIdDemo+"' and g.demouserid ='"+useridDemo+"'\n" + 
				 		"where  a.companyId= '"+companyIdDemo+"' and uuid='"+id+"' ");
				this.getJdbcTemplate_newg().update(sb1.toString());
				
				//插入到记录表
				ShowData data=new ShowData();
				data.setTablename("log_claim");
				data.setKey("uuid");
				data.setKeyvalue(idnew);
				data.setDemoKeyvalue(id);
				data.setCompanyid(companyIdnew);
				data.setUserid(useridnew);
				data.setDemocompanyid(companyIdDemo);
				data.setDemouserid(useridDemo);
				data.setKeytype("mainkey");
				insertDemodata(data);
				
		}
			//插入日志log_operate
		 sb.delete(0, sb.length());
		 sb.append("select `uuid`, `clueId`, `customerId`, `opportunityId`, `projectId`, `type`, `userid`, `username`, `companyId`, `createTime`, `remark`, `memo`, `sqllog`\n" + 
		 		"from log_operate  where  companyId= '"+companyIdDemo+"' ");
			List<Map<String, Object>> operateList = this.getJdbcTemplate_newg().queryForList(sb.toString(), new Object[]{});
		 for (int i=0;i<operateList.size();i++) {
				 String id=(String)operateList.get(i).get("uuid");
				 String idnew = UUID.randomUUID().toString();
				 sb1.delete(0, sb1.length());
				 sb1.append("INSERT INTO `log_operate` (`uuid`, `clueId`, `customerId`, `opportunityId`, `projectId`, `type`, `userid`, `username`, `companyId`, `createTime`, `remark`, `memo`, `sqllog`) \n" + 
				 	    "select '"+idnew+"', b.keyvalue, c.keyvalue, d.keyvalue, e.keyvalue, `type`, if(f.keyvalue is null  or f.keyvalue='','"+useridnew+"',f.keyvalue),  if(g.keyvalue is null  or g.keyvalue='','"+usernamenew+"',g.keyvalue), '"+companyIdnew+"', `createTime`, `remark`, `memo`, `sqllog` " + 
				 		"from log_operate a\n" + 
				 		"left JOIN d_demodata b on a.clueId = b.demoKeyvalue and b.tablename='clue' and  b.`key`= 'uuid' and  b.democompanyid = '"+companyIdDemo+"' and b.demouserid ='"+useridDemo+"'\n" + 
				 		"left JOIN d_demodata c on a.customerId = c.demoKeyvalue and c.tablename='customer' and  c.`key`= 'uuid' and  c.democompanyid = '"+companyIdDemo+"' and c.demouserid ='"+useridDemo+"'\n" + 
				 		"left JOIN d_demodata d on a.opportunityId = d.demoKeyvalue and d.tablename='opportunity' and  d.`key`= 'uuid' and  d.democompanyid = '"+companyIdDemo+"' and d.demouserid ='"+useridDemo+"'\n" + 
				 		"left JOIN d_demodata e on a.projectId = e.demoKeyvalue and e.tablename='t_project' and  e.`key`= 'uuid' and  e.democompanyid = '"+companyIdDemo+"' and e.demouserid ='"+useridDemo+"'\n" + 
				 		"left JOIN d_demodata f on a.userid = f.demoKeyvalue and f.tablename='t_user' and  f.`key`= 'id' and  f.democompanyid = '"+companyIdDemo+"' and f.demouserid ='"+useridDemo+"'\n" + 
				 		"left JOIN d_demodata g on a.username = g.demoKeyvalue and g.tablename='t_user' and  g.`key`= 'username' and  g.democompanyid = '"+companyIdDemo+"' and g.demouserid ='"+useridDemo+"'\n" + 
				 		"where  a.companyId= '"+companyIdDemo+"' and uuid='"+id+"' ");
				this.getJdbcTemplate_newg().update(sb1.toString());
				
				//插入到记录表
				ShowData data=new ShowData();
				data.setTablename("log_claim");
				data.setKey("uuid");
				data.setKeyvalue(idnew);
				data.setDemoKeyvalue(id);
				data.setCompanyid(companyIdnew);
				data.setUserid(useridnew);
				data.setDemocompanyid(companyIdDemo);
				data.setDemouserid(useridDemo);
				data.setKeytype("mainkey");
				insertDemodata(data);
				
		}

		 res.setRetMessage("OK!"); 
		res.setRetCode(0);
  		return res;
  	}
    

  	
  	
  	
    @SuppressWarnings("unchecked")
	public PageData calUserPoints() {
    	PageData res = new PageData();
    	res.setRetCode(9);
		res.setRetMessage("获取信息失败");
    	StringBuffer sb = new StringBuffer();
    	String StartTime="";
    	String EndTime="";
		newg_status = begin_newg();// 开启事物


		// 获取昨天一天
		SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
		Calendar cal_1 = Calendar.getInstance();// 获取当前日期
		cal_1.add(Calendar.DATE, -1);
		String strdate=format1.format(cal_1.getTime());
		StartTime = format1.format(cal_1.getTime())+ " 00:00:00";
		EndTime=format1.format(cal_1.getTime())+ " 23:59:59";
		
		try {
			//获取规则
			sb.delete(0, sb.length()) ;
			sb.append("SELECT a.uuid , a.company_id, b.id uid,b.phone userPhone, a.uuid rule_uuid,'' link_type,'' link_id, a.point_type2, a.point_type4, a.point_type6, c.name title , d.name sub_title, 'remark',d.sql exsql , a.point_unit,points, 0 userpoints  "); 
			sb.append(" from t_user_points_rule a  inner join t_user b  on a.company_id=b.companyId and b.sfsc='0' ");
			sb.append("INNER JOIN t_param_level c on a.point_type4=c.`value` ");
			sb.append("INNER JOIN t_param_level d on a.point_type6=d.`value`   where b.id='131'");
			List<Map<String, Object>> pointsRule = this.getJdbcTemplate_newg()
					.queryForList(sb.toString());
			if(pointsRule.size()>0){
				for(int i=0;i<pointsRule.size();i++){
					String selsql=(String)pointsRule.get(i).get("exsql");
					String userId=String.valueOf((Long)pointsRule.get(i).get("uid"));
					String userPhone=(String)pointsRule.get(i).get("userPhone");
					
					//增加开始截止时间
					if (selsql!=null && !"".equals(selsql) && userId!=null && !"".equals(userId))
					{
						selsql=selsql.replace("userId", userId);
						selsql=selsql.replace("userPhone", userPhone);
						selsql=selsql.replace("StartTime", StartTime);
						selsql=selsql.replace("EndTime", EndTime);
					sb.delete(0, sb.length()) ;
					sb.append(selsql);
					List<Map<String, Object>>   ret = this.getJdbcTemplate_newg().queryForList(sb.toString());
					if (ret.size()>0){
						for (int j=0;j<ret.size();j++){
							BigDecimal num = new BigDecimal("0");
							BigDecimal points = new BigDecimal("0");
							BigDecimal point_unit = new BigDecimal("0");
							BigDecimal userpoints = new BigDecimal("0");
							
							Object obj=(Object)ret.get(j).get("num");
							if(obj instanceof Long) {
								 num = BigDecimal.valueOf((Long)obj);
							}else if(obj instanceof BigDecimal) {
								num=(BigDecimal)obj;
							}
							String link_id="";
							Object obj1=(Object)ret.get(j).get("link_id");
							if(obj1 instanceof Long) {
								link_id = String.valueOf((Long)obj1);
							}else if(obj1 instanceof String) {
								link_id=(String)obj1;
							}
							if (!num.equals(BigDecimal.ZERO)) {
							
							points=(BigDecimal)pointsRule.get(i).get("points");
							point_unit=(BigDecimal)pointsRule.get(i).get("point_unit");
							
							userpoints=num.divide(point_unit, 2, RoundingMode.HALF_UP).multiply(points);
							
							sb.delete(0, sb.length()) ;
							sb.append("INSERT INTO `t_user_points` ( `uuid`, `company_id`, `uid`, `rule_uuid`, `link_type`, `link_id`, `point_type2`,`point_type4`,`point_type6`, `title`, `sub_title`, `remark`, `basenum`,point_unit, `points`,userpoints, `summaryTime`,  `createUid`, `createTime`, `modifyUid`, `modifyTime`)");
							sb.append(" values ( UUID(),'"+Objects.toString((String)pointsRule.get(i).get("company_id"),"")+"', ");
							sb.append(" '"+userId+"', ");
							sb.append(" '"+Objects.toString((String)pointsRule.get(i).get("rule_uuid"),"")+"', ");
							sb.append(" '"+Objects.toString((String)ret.get(j).get("link_type"),"")+"', ");
							sb.append(" '"+link_id+"', ");
							sb.append(" '"+Objects.toString((String)pointsRule.get(i).get("point_type2"),"")+"', ");
							sb.append(" '"+Objects.toString((String)pointsRule.get(i).get("point_type4"),"")+"', ");
							sb.append(" '"+Objects.toString((String)pointsRule.get(i).get("point_type6"),"")+"', ");
							sb.append(" '"+Objects.toString((String)pointsRule.get(i).get("title"),"")+"', ");
							sb.append(" '"+Objects.toString((String)pointsRule.get(i).get("sub_title"),"")+"', ");
							sb.append(" '"+Objects.toString((String)pointsRule.get(i).get("remark"),"")+"', ");
							sb.append(" '"+Objects.toString(num,"")+"', ");
							sb.append(" '"+Objects.toString((BigDecimal)pointsRule.get(i).get("point_unit"),"")+"', ");
							sb.append(" '"+Objects.toString((BigDecimal)pointsRule.get(i).get("points"),"")+"', ");
							sb.append(" '"+Objects.toString(userpoints,"")+"', ");
							sb.append(" '"+strdate+"', ");
							sb.append(" '99999', ");
							sb.append(" now(), ");
							sb.append(" '99999', ");
							sb.append(" now() ); ");
							this.getJdbcTemplate_newg().update(sb.toString());
						}
					
						}
					}
					}
				}
			}
			res.setRetCode(0);
			res.setRetMessage("获取信息成功");
			commit_newg(newg_status);
		} catch (Exception e) {
			rollback_newg(newg_status);
			e.printStackTrace();
		} finally {
			
		}
		return res;
    }
    
 
	
}
